Java Collection Add addIgnoreNull(final Collection collection, final T object)

Here you can find the source of addIgnoreNull(final Collection collection, final T object)

Description

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

License

Apache License

Parameter

Parameter Description
T the type of object the Collection contains
collection the collection to add to, must not be null
object the object to add, if null it will not be added

Exception

Parameter Description
NullPointerException if the collection is null

Return

true if the collection changed

Declaration

public static <T> boolean addIgnoreNull(final Collection<T> collection, final T object) 

Method Source Code

//package com.java2s;
/*/*  w  ww.  jav a  2s  .  c  om*/
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.Collection;

public class Main {
    /**
     * Adds an element to the collection unless the element is null.
     *
     * @param <T>  the type of object the {@link Collection} contains
     * @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
     * @since 3.2
     */
    public static <T> boolean addIgnoreNull(final Collection<T> collection, final T object) {
        if (collection == null) {
            throw new NullPointerException("The collection must not be null");
        }
        return object != null && collection.add(object);
    }
}

Related

  1. addIfNotNull(Collection coll, T value)
  2. addIfNotNull(Collection listToAddTo, T itemToAddToList, Object objectToCheckIfNull)
  3. addIfNotNull(final Collection collection, final V value)
  4. addIgnoreNull(Collection collection, Object object)
  5. addIgnoreNull(Collection collection, T object)
  6. addInClauseToQuery(StringBuilder query, Collection l)
  7. addItem(Collection collection, Object item)
  8. addMatchingStrings(Collection availableProperties, String startWith, Collection strings)
  9. addNewLines(Collection strings)

  10. HOME | Copyright © www.java2s.com 2016