Java Collection Add addToCollection(Collection collection, final T value)

Here you can find the source of addToCollection(Collection collection, final T value)

Description

Adds value to the Collection .

License

Apache License

Parameter

Parameter Description
collection Collection into which the value is added into; can be null
value Value of type <T>; can be null

Declaration

public static <T> void addToCollection(Collection<T> collection, final T value) 

Method Source Code


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

import java.util.Collection;

public class Main {
    /**//from  w  w w. j a  va2s  .  c o  m
     * Adds value to the {@link Collection}. The value of type <T> is added to
     * the {@link Collection} if the {@link Collection} or the value are not
     * null. If {@link Collection#add(Object)} throws
     * {@link IllegalStateException} it is caught and silently ignored.
     * 
     * @param collection
     *            {@link Collection} into which the value is added into; can be
     *            null
     * @param value
     *            Value of type <T>; can be null
     */
    public static <T> void addToCollection(Collection<T> collection, final T value) {
        if (collection != null && value != null) {
            try {
                collection.add(value);
            } catch (IllegalStateException e) {
                // Silently fail
            }
        }
    }
}

Related

  1. addSafe(final Collection collection, T element)
  2. addTo(Collection l, Object... os)
  3. addTo(E element, C collection)
  4. addTo(Map> map, K key, V value)
  5. addToCollection(C collection, E... elements)
  6. addToCollection(Collection collection, T obj)
  7. addToCollection(Collection theCollection, T... objects)
  8. addToCollection(Iterator iterator, Collection collection)
  9. addToCollection(T dest, boolean failOnNull, Iterable... srcs)