Java Utililty Methods Set Union

List of utility methods to do Set Union

Description

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

Method

Setunion(final Iterable> elements)
union
return new HashSet<T>() {
    private static final long serialVersionUID = -3161916411604210423L;
        for (final Set<T> s : elements) {
            addAll(s);
};
...
Setunion(final Set set1, final Set set2)
union
Set<E> set = new HashSet<>(set1);
set.addAll(set2);
return Collections.unmodifiableSet(set);
Setunion(final Set... elements)
union
return ((elements.length == 0) ? Collections.<T>emptySet()
        : new HashSet<T>(elements.length * elements[0].size()) {
            private static final long serialVersionUID = -3161916411604210423L;
                for (final Set<T> s : elements) {
                    addAll(s);
        });
Setunion(Set one, Set two)
Form a new set that is the union of two IntSets.
HashSet n = new HashSet(one.size() + two.size());
Iterator it = one.iterator();
while (it.hasNext()) {
    n.add(it.next());
it = two.iterator();
while (it.hasNext()) {
    n.add(it.next());
...
Setunion(Set a, Set b)
Returns union of a and b as a Hashtable containing all elements in a and b
if (a == null || b == null || a.size() == 0 || b.size() == 0)
    return Collections.EMPTY_SET;
Set<Object> union = new HashSet<Object>((a.size() < b.size()) ? b : a);
Set<?> src = (a.size() < b.size()) ? b : a;
for (Object o : src) {
    if (!union.contains(o))
        union.add(o);
return union;
Setunion(Set left, Set right)
union
Set<T> smaller;
Set<T> bigger;
if (left.size() > right.size()) {
    bigger = left;
    smaller = right;
} else {
    bigger = right;
    smaller = left;
...
Setunion(Set setA, Set setB)
Union.
if (isEmpty(setA) && !isEmpty(setB)) {
    return Collections.unmodifiableSet(setB);
if (!isEmpty(setA) && isEmpty(setB)) {
    return Collections.unmodifiableSet(setA);
if (isEmpty(setA) && isEmpty(setB)) {
    return new LinkedHashSet<T>();
...
Setunion(Set setA, Set setB)
This method finds the union of set A and set B
Set<T> union = new HashSet<T>(setA);
union.addAll(setB);
return union;
Setunion(Set... sets)
Returns the union of multiple sets.
if (sets == null)
    return new HashSet<>();
Set<T> result = new HashSet<>();
for (Set<T> s : sets) {
    if (s != null)
        result.addAll(s);
return result;
...
String[]union(String[] set1, String[] set2)
Same as union, but for Strings.
String[] set = new String[set1.length + set2.length];
System.arraycopy(set1, 0, set, 0, set1.length);
System.arraycopy(set2, 0, set, set1.length, set2.length);
return (String[]) unique(set);