Example usage for java.util Collection add

List of usage examples for java.util Collection add

Introduction

In this page you can find the example usage for java.util Collection add.

Prototype

boolean add(E e);

Source Link

Document

Ensures that this collection contains the specified element (optional operation).

Usage

From source file:Main.java

public static final <E> void addToCollection(Collection<? super E> collection, E... elements) {
    for (E e : elements) {
        collection.add(e);
    }// w w w .j av  a  2s . co m
}

From source file:Main.java

/**
 * Adds an element to the collection unless the element is null.
 *
 * @param collection  the collection to add to, must not be null
 * @param object  the object to add, if null it will not be added
 * @return true if the collection changed
 * @throws NullPointerException if the collection is null
 * @since Commons Collections 3.2// w  w w . j  a  v  a 2 s .c om
 */
public static boolean addIgnoreNull(Collection collection, Object object) {
    return (object == null ? false : collection.add(object));
}

From source file:Main.java

public static <T> void addIfNotNull(Collection<T> c, T element) {
    if (null != c && null != element) {
        c.add(element);
    }/*from w  w  w  .  jav  a2s .c o  m*/
}

From source file:Main.java

private static <T> void iterableToCollection(Iterable<? extends T> c, Collection<T> list) {
    for (T element : c) {
        list.add(element);
    }//from w ww . j a v  a2  s  .co  m
}

From source file:Main.java

public static <C extends Collection<T>, T> Collection<T> fromArray(T[] array, Class<C> clazz) {
    try {//from   w  w w.ja  v  a2  s  . c o  m
        Collection<T> collection = clazz.newInstance();
        for (T o : array) {
            collection.add(o);
        }
        return collection;
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

/**
 * Add all elements from the supplied array to the specified collection.
 *
 * @param collection The collection to add to.
 * @param array The elements to add./* w  w w  .jav a 2s  . c o m*/
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public static void addAll(Collection collection, Object[] array) {
    for (Object obj : array) {
        collection.add(obj);
    }
}

From source file:Main.java

/**
 * Similar to Collection.add() but only allows non-null elements.
 *
 * @return <code>true</code> if this collection changed as a result of the call
 *///w ww  . ja va  2 s .  com
public static <T> boolean addSafe(final Collection<T> collection, T element) {
    if (element != null) {
        return collection.add(element);
    } else {
        return false;
    }
}

From source file:Main.java

private static void findNodesNamed(Node node, String lookForName, Collection<Node> ret) {
    if (node.getNodeName().equals(lookForName)) {
        ret.add(node);
    } else {//from   ww  w  .  j  a va2  s .  c om
        NodeList list = node.getChildNodes();
        for (int i = 0; i < list.getLength(); ++i) {
            findNodesNamed(list.item(i), lookForName, ret);
        }
    }
}

From source file:Main.java

public static <O, P extends O> boolean safeAddAll(Collection<O> c, P[] items) {
    try {//from   w w w.  j av a  2 s . com
        for (O item : items) {
            c.add(item);
        }

        return true;
    } catch (Throwable ex) {
        return false;
    }
}

From source file:Main.java

public static Collection<Thread> getThreads(int count, Supplier<Thread> threadSupplier) {
    Collection<Thread> threads = new ArrayList<>(count);
    for (int i = 0; i < count; i++) {
        threads.add(threadSupplier.get());
    }/*from ww  w. j  a  va  2 s .  c  o  m*/
    return threads;
}