Java Collection Add addToCollection(C collection, E... elements)

Here you can find the source of addToCollection(C collection, E... elements)

Description

Adds elements to a collection.

License

LGPL

Parameter

Parameter Description
C the collection type
E the elements type
collection the collection to add the elements to. The collection implementation should support <code>null</code> values if such are expected in the second argument.
elements a list of elements to add to the specified collection. If any of the elements is <code>null</code> it will be added to the collection.

Return

the same collection instance as the argument with added elements

Declaration

@SafeVarargs
public static <E, C extends Collection<E>> C addToCollection(C collection, E... elements) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import java.util.Collection;

public class Main {
    /**//from   w  w  w  .j  ava 2 s .  co  m
     * Adds elements to a collection. The method modifies the supplied collection by adding the elements from the
     * variable arguments parameter if any.
     *
     * @param <C>
     *            the collection type
     * @param <E>
     *            the elements type
     * @param collection
     *            the collection to add the elements to. The collection implementation should support <code>null</code>
     *            values if such are expected in the second argument.
     * @param elements
     *            a list of elements to add to the specified collection. If any of the elements is <code>null</code> it
     *            will be added to the collection.
     * @return the same collection instance as the argument with added elements
     */
    @SafeVarargs
    public static <E, C extends Collection<E>> C addToCollection(C collection, E... elements) {
        if (collection == null || elements == null || elements.length == 0) {
            return collection;
        }
        for (E e : elements) {
            collection.add(e);
        }
        return collection;
    }
}

Related

  1. addRange(Collection c, int start, int to, int step)
  2. addSafe(final Collection collection, T element)
  3. addTo(Collection l, Object... os)
  4. addTo(E element, C collection)
  5. addTo(Map> map, K key, V value)
  6. addToCollection(Collection collection, final T value)
  7. addToCollection(Collection collection, T obj)
  8. addToCollection(Collection theCollection, T... objects)
  9. addToCollection(Iterator iterator, Collection collection)