union Of List Of Lists - Java java.util

Java examples for java.util:List Operation

Description

union Of List Of Lists

Demo Code


//package com.java2s;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

public class Main {
  public static void main(String[] argv) {
    Collection collectionOfCollection = java.util.Arrays.asList("asdf", "java2s.com");
    System.out.println(unionOfListOfLists(collectionOfCollection));
  }//from www  .  j  av a2  s  .c o  m

  /**
   * @param collectionOfCollection
   *          a Collection<Collection<T>>
   * 
   * @return a Set<T> containing all values of all Collections<T> without
   *         any duplicates
   */
  public static <T> Set<T> unionOfListOfLists(final Collection<? extends Collection<T>> collectionOfCollection) {
    if (collectionOfCollection == null || collectionOfCollection.isEmpty()) {
      return new HashSet<T>(0);
    }
    final HashSet<T> union = new HashSet<T>();
    for (final Collection<T> col : collectionOfCollection) {
      if (col != null) {
        for (final T t : col) {
          if (t != null) {
            union.add(t);
          }
        }
      }
    }
    return union;
  }

}

Related Tutorials