Adds an element to the collection unless the element is null. - Java java.util

Java examples for java.util:Collection Null Element

Description

Adds an element to the collection unless the element is null.

Demo Code


//package com.java2s;

import java.util.Collection;

public class Main {
    /**/*from  w ww  .  j av a 2  s  . c om*/
     * Adds an element to the collection unless the element is null.
     * 
     * @param collection
     *            the collection to add to, must not be null
     * @param object
     *            the object to add, if null it will not be added
     * @return true if the collection changed
     * @throws NullPointerException
     *             if the collection is null
     */
    public static <E> boolean addIgnoreNull(Collection<E> collection,
            E object) {
        return (object == null ? false : collection.add(object));
    }
}

Related Tutorials