Java Collection Union union(Collection c1, Collection c2)

Here you can find the source of union(Collection c1, Collection c2)

Description

intersection of c1 and c2, does not change input collections, returns new collection (list)

License

Open Source License

Declaration

public static <T> List<T> union(Collection<T> c1, Collection<T> c2) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;

import java.util.Collection;
import java.util.Collections;

import java.util.List;

public class Main {
    /**//  w  w  w .  jav  a2 s.co m
     * intersection of c1 and c2, does not change input collections, returns new collection (list)
     */
    public static <T> List<T> union(Collection<T> c1, Collection<T> c2) {
        if (c1 == null && c2 == null) {
            return Collections.emptyList();
        }
        if (c1 == null) {
            return new ArrayList<>(c2);
        }
        if (c2 == null) {
            return new ArrayList<>(c1);
        }
        List<T> tmp = new ArrayList<>(c1.size() + c2.size());
        tmp.addAll(c1);
        tmp.addAll(c2);
        return tmp;
    }

    public static <T> List<T> emptyList() {
        return Collections.emptyList();
    }
}

Related

  1. union(Collection coll)
  2. union(Collection collection1, Collection collection2)
  3. union(Collection c1, Collection c2)
  4. union(Collection> sets)
  5. union(Collection a, Collection b)
  6. union(Collection initial, Collection other)
  7. union(Collection lhs, Collection rhs)
  8. union(Collection set1, Collection set2)
  9. union(final Collection a, final Collection b)