Java Collection Unique addUnique(final Collection collection, final Object value)

Here you can find the source of addUnique(final Collection collection, final Object value)

Description

add a unique value to a collection.

License

Open Source License

Parameter

Parameter Description
collection the collection.
value the value.

Return

boolean.

Declaration

public static boolean addUnique(final Collection collection,
        final Object value) 

Method Source Code

//package com.java2s;

import java.util.Collection;

public class Main {
    /**/*from ww  w.  java 2s .com*/
     * 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. addUnique(C collection, T item)
  2. generateUniqueName(String aName, Collection aStringCollection)
  3. getUnique(Collection c)
  4. getUnique(Collection collection)
  5. getUniqueName(String name, Collection collection)