Java Collection Add addIfNotNull(Collection coll, T value)

Here you can find the source of addIfNotNull(Collection coll, T value)

Description

Adds a value to the collection if it's not null.

License

Apache License

Declaration

public static <T> Collection<T> addIfNotNull(Collection<T> coll, T value) 

Method Source Code

//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

import java.util.Collection;

public class Main {
    /**/*from   ww  w  . j av a2 s  .co  m*/
     *  Adds a value to the collection if it's not null. Returns the collection
     *  as a convenience for chained invocations.
     *
     *  @since 1.0.11
     */
    public static <T> Collection<T> addIfNotNull(Collection<T> coll, T value) {
        return addIf(coll, value, value != null);
    }

    /**
     *  Adds a value to the collection if the boolean expression is true.
     *  Returns the collection as a convenience for chained invocations.
     *
     *  @since 1.0.8
     */
    public static <T> Collection<T> addIf(Collection<T> coll, T value, boolean expr) {
        if (expr)
            coll.add(value);

        return coll;
    }
}

Related

  1. addIfAbsent(Collection c, T item)
  2. addIfMissing(Collection result, Iterable addition)
  3. addIfNotContains(Collection collection, T value)
  4. addIfNotEmpty(Collection c, Collection elements)
  5. addIfNotNull(Collection c, Object element)
  6. addIfNotNull(Collection listToAddTo, T itemToAddToList, Object objectToCheckIfNull)
  7. addIfNotNull(final Collection collection, final V value)
  8. addIgnoreNull(Collection collection, Object object)
  9. addIgnoreNull(Collection collection, T object)