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

TaddAll(T coll, Collection other)
add All
coll.addAll(other);
return coll;
voidaddAll(T[] lhs, Collection rhs)
Add all elements from an array into a given collection of the same type.
for (int i = 0; i != lhs.length; ++i) {
    rhs.add(lhs[i]);
voidaddAllFirst(Collection col, Deque deque)
add All First
for (T item : col) {
    deque.addFirst(item);
booleanaddAllIfNotNull(final Collection collection, final Collection c)
add All If Not Null
return (collection != null) && (c != null) && collection.addAll(c);
voidaddAllIfNotNull(final Collection collection, final Collection values)
add All If Not Null
if (collection != null && values != null) {
    collection.addAll(values);
booleanaddAllIfSet(Collection target, Collection c)
Adds all of the elements in the specified collection to the target collection if a collection is specified.
if (c == null)
    return false;
return target.addAll(c);
booleanaddAllIgnoreNull(Collection c, T... elements)
Null elements are not taking into account.
boolean result = false;
for (T element : elements) {
    if (element != null) {
        result |= c.add(element);
return result;
booleanaddAllIgnoreNull(final Collection target, final Collection source)
Add elements from a source collection to a target collection unless they or the source collection itself are null.
if (target == null)
    throw new NullPointerException("The target collection must not be null");
if (source == null)
    return false;
boolean changed = false;
for (T element : source)
    changed |= addIgnoreNull(target, element);
return changed;
...
voidaddAllNotNull(final Collection c, final E... elements)
adds all not null elements to the specified collection
if (elements == null) {
    return;
for (final E e : elements) {
    addIfNotNull(e, c);
CollectionaddAllOrSame(Collection collection, Collection newElements)
Adds to a collection all given new elements in a second collection, returning the second collection same instance if the first is null or empty.
Collection<T> result;
if (collection == null || collection.isEmpty()) {
    result = newElements;
} else {
    collection.addAll(newElements);
    result = collection;
return result;
...