Java Collection Union union(final Collection c1, final Collection c2)

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

Description

union

License

Open Source License

Declaration

public static Collection union(final Collection c1, final Collection c2) 

Method Source Code

//package com.java2s;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

public class Main {
    public static Collection union(final Collection c1, final Collection c2) {
        final List list = new ArrayList();
        addAllUnique(list, c1);/* www  .j  ava 2s  .c  o m*/
        addAllUnique(list, c2);
        return list;
    }

    /**
     * add a collection of values to a collection.
     * 
     * @param collection
     *            the collection.
     * @param values
     *            the values.
     * @return boolean.
     * @since 0.1
     */
    public static boolean addAllUnique(final Collection collection,
            final Collection values) {
        boolean changed = false;
        for (final Iterator iter = values.iterator(); iter.hasNext();) {
            changed = addUnique(collection, iter.next());
        }
        return changed;
    }

    /**
     * add a unique value to a collection. If the value already exists, it will
     * return <code>false</code>.
     * 
     * @param collection
     *            the collection.
     * @param value
     *            the value.
     * @return boolean.
     * @since 0.1
     */
    public static boolean addUnique(final Collection collection,
            final Object value) {
        if (collection.contains(value)) {
            return false;
        }
        return collection.add(value);
    }
}

Related

  1. union(Collection c1, Collection c2)
  2. union(Collection initial, Collection other)
  3. union(Collection lhs, Collection rhs)
  4. union(Collection set1, Collection set2)
  5. union(final Collection a, final Collection b)
  6. union(final Collection... sets)
  7. union(final Collection a, final Collection b)
  8. unionAll(final Collection collector, final T[] a, final T[] b)