Java Utililty Methods Collection Union

List of utility methods to do Collection Union

Description

The list of methods to do Collection Union are organized into topic(s).

Method

Listunion(Collection c1, Collection c2)
union
List collection = new ArrayList();
collection.addAll(c1);
collection.addAll(c2);
return collection;
Setunion(Collection coll)
Returns the union of all the sets given in a collection.
Set set = new HashSet();
for (Iterator i = coll.iterator(); i.hasNext();) {
    set.addAll((Collection) i.next());
return set;
Collectionunion(Collection collection1, Collection collection2)
Given two collections of elements of type , return a collection containing the items from both lists
if (isEmpty(collection1)) {
    if (isEmpty(collection2)) {
        return Collections.emptyList();
    return new ArrayList<T>(collection2);
if (isEmpty(collection2)) {
    return new ArrayList<T>(collection1);
...
Listunion(Collection c1, Collection c2)
Adds all elements of two collections to a new LinkedList.
List<E> result = new LinkedList<E>();
result.addAll(c1);
result.addAll(c2);
return result;
Setunion(Collection> sets)
Determines the union of a collection of sets.
Set<T> result = new HashSet<>();
if (sets.isEmpty()) {
    return result;
Iterator<Set<T>> iter = sets.iterator();
result.addAll(iter.next());
if (sets.size() == 1) {
    return result;
...
Collectionunion(Collection a, Collection b)
Union of two collections with duplicates
return null;
Listunion(Collection c1, Collection c2)
intersection of c1 and c2, does not change input collections, returns new collection (list)
if (c1 == null && c2 == null) {
    return Collections.emptyList();
if (c1 == null) {
    return new ArrayList<>(c2);
if (c2 == null) {
    return new ArrayList<>(c1);
...
Listunion(Collection initial, Collection other)
returns a List of union objects, i.e.
ArrayList<T> union = new ArrayList<T>();
for (T obj : initial) {
    if (other.contains(obj))
        union.add(obj);
return union;
Setunion(Collection lhs, Collection rhs)
union
HashSet<T> results = new HashSet<>(lhs.size() + rhs.size());
results.addAll(lhs);
results.addAll(rhs);
return results;
Collectionunion(Collection set1, Collection set2)
union
Collection<T> union = new ArrayList<>();
for (T t : set1) {
    union.add(t);
for (T t : set2) {
    union.add(t);
return union;
...