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

voidaddNonNull(Object element, Collection result)
add Non Null
if (element != null) {
    result.add(element);
voidaddNonnullIfMissing(Collection collection, T element)
Adds an element to a collection only if the latter does not contain it and the element is not null.
if (collection == null)
    throw new IllegalArgumentException();
if (element != null && !collection.contains(element)) {
    collection.add(element);
CollectionaddNonNulls(Collection c, X... xl)
add those elements which are not null to the given collection and return it
for (X x : xl) {
    if (x != null)
        c.add(x);
return c;
voidaddNotNull(Collection col, String toAdd)
add Not Null
if (toAdd != null && toAdd.length() > 0 && !col.contains(toAdd.trim())) {
    col.add(toAdd.trim());
voidaddOneValueUNSAFE(Collection cloneCollection, Object v)
add One Value UNSAFE
cloneCollection.add(v);
CollectionaddPrefix(Collection values, String prefix)
add Prefix
Collection<String> newValues = new ArrayList<String>();
for (String value : values) {
    newValues.add(prefix + value);
return newValues;
voidaddPreviousTags(int index, String[] prevTags, int prevTagsToAdd, Collection targetCol)
add Previous Tags
if (prevTags == null) {
    prevTags = EMPTY_STRING_ARRAY;
if (index - 1 >= prevTags.length) {
    throw new IllegalStateException();
for (int pt = 1; pt <= prevTagsToAdd; pt++) {
    int t = index - pt;
...
voidaddRange(Collection c, int start, int to, int step)
Adds values into the given collection using integer in the specified range and step size.
if (step <= 0)
    throw new RuntimeException("Would create an infinite loop");
for (int i = start; i < to; i += step)
    c.add(i);
booleanaddSafe(final Collection collection, T element)
Similar to Collection.add() but only allows non-null elements.
if (element != null) {
    return collection.add(element);
} else {
    return false;
TaddTo(Collection l, Object... os)
add To
Collections.addAll(l, os);
return (T) l;