List大联合

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class UnionOfList {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Collection collectionOfCollection = java.util.Arrays.asList("asdf",
            "java");
    System.out.println(unionOfListOfLists(collectionOfCollection));
}
/**
 * @param collectionOfCollection
 *            a Collection<Collection<T>>
 * 
 * @return a Set<T> containing all values of all Collections<T>
 *         without any duplicates
 */
public static  Set unionOfListOfLists(
        final Collection> collectionOfCollection) {
    if (collectionOfCollection == null
            || collectionOfCollection.isEmpty()) {
        return new HashSet(0);
    }
    final HashSet union = new HashSet();
    for (final Collection col : collectionOfCollection) {  //有一点问题
        if (col != null) {
            for (final T t : col) {
                if (t != null) {
                    union.add(t);
                }
            }
        }
    }
    return union;
}

public static  List asList(final Iterable iterable) {
    return (iterable instanceof Collection) ? new LinkedList(
            (Collection) iterable) : new LinkedList() {
        private static final long serialVersionUID = 3109256773218160485L;
        {
            if (iterable != null) {
                for (final T t : iterable) {
                    add(t);
                }
            }
        }
    };
}

public static  List asList(final T t, final T... ts) {
    final ArrayList list = new ArrayList(ts.length + 1);
    list.add(t);
    Collections.addAll(list, ts);
    return list;
}

/**
 * Check if collection is not null and empty
 * 
 * @param collection
 *          Collection to check
 * 
 * @return true, if collection is not null and empty, else false
 */
public static  boolean isEmpty(final Collection collection) {
    return collection != null && collection.isEmpty();
}

/**
 * Check if map is not null and empty
 * 
 * @param map
 *          map to check
 * 
 * @return true, if map is not null and empty, else false
 */
public static  boolean isEmpty(final Map map) {
    return map != null && map.isEmpty();
}

}

你可能感兴趣的:(List大联合)