Java Utililty Methods Collection Add

List of utility methods to do Collection Add

Description

The list of methods to do Collection Add are organized into topic(s).

Method

CaddTo(E element, C collection)
Adds the given element to the given collection.
collection.add(element);
return collection;
voidaddTo(Map> map, K key, V value)
takes in a map K,List and adds value to key's list - effectively a multi-map.
Collection<V> values = map.get(key);
if (values == null) {
    values = new ArrayList<V>();
    map.put(key, values);
values.add(value);
CaddToCollection(C collection, E... elements)
Adds elements to a collection.
if (collection == null || elements == null || elements.length == 0) {
    return collection;
for (E e : elements) {
    collection.add(e);
return collection;
voidaddToCollection(Collection collection, final T value)
Adds value to the Collection .
if (collection != null && value != null) {
    try {
        collection.add(value);
    } catch (IllegalStateException e) {
ListaddToCollection(Collection collection, T obj)
add To Collection
List<T> list = null;
if (null == collection) {
    list = new ArrayList<>();
    list.add(obj);
} else {
    collection.add(obj);
return list;
...
voidaddToCollection(Collection theCollection, T... objects)
add To Collection
for (T object : objects) {
    theCollection.add(object);
CollectionaddToCollection(Iterator iterator, Collection collection)
add To Collection
while (iterator.hasNext()) {
    T object = iterator.next();
    collection.add(object);
return Collections.unmodifiableCollection(collection);
TaddToCollection(T dest, boolean failOnNull, Iterable... srcs)
add To Collection
for (Iterable<? extends V> src : srcs) {
    for (V v : src) {
        if (failOnNull && v == null) {
            throw new IllegalArgumentException("Illegal null value provided in this collection: " + src);
        dest.add(v);
return dest;
voidaddToCollectionIfNotNull(Collection collection, Object value)
add To Collection If Not Null
if (value != null && collection != null) {
    collection.add(value);
voidaddToString(final Collection output, final T element, final boolean stringifyNull)
add To String
if (element != null || stringifyNull) {
    output.add(String.valueOf(element));
} else {
    output.add(null);