Java Collection Add addAllUnique(final Collection collection, final Collection values)

Here you can find the source of addAllUnique(final Collection collection, final Collection values)

Description

add a collection of values to a collection.

License

Open Source License

Parameter

Parameter Description
collection the collection.
values the values.

Return

boolean.

Declaration

public static boolean addAllUnique(final Collection collection,
        final Collection values) 

Method Source Code

//package com.java2s;

import java.util.Collection;
import java.util.Iterator;

public class Main {
    /**// ww  w.j a  v a 2s  .  com
     * 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. addAllIgnoreNull(final Collection target, final Collection source)
  2. addAllNotNull(final Collection c, final E... elements)
  3. addAllOrSame(Collection collection, Collection newElements)
  4. addAllSafe(Collection collection1, Collection collection2)
  5. addAllUncontained(Collection col1, Collection col2)
  6. addArray(Collection col, T[] array)
  7. addArray(java.util.Collection v, T array[])
  8. addArrayToCollection(T[] array, Collection collection)
  9. addArrayToCollection(T[] array, Collection collection)