Java Collection First getFirstNotNullValue(final Collection collection)

Here you can find the source of getFirstNotNullValue(final Collection collection)

Description

Returns the first not null element if the collection is not null and have not null value, else return null.

License

Apache License

Parameter

Parameter Description
collection the collection to be handled.

Return

the first not null element if the collection is not null and have not null value, else null.

Declaration

public static <T> T getFirstNotNullValue(final Collection<T> collection) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.Collection;

public class Main {
    /**/*from   ww  w . j a  v  a 2 s  .c om*/
     * Returns the first not null element if the collection is not null and have
     * not null value, else return null.
     * 
     * @param collection
     *            the collection to be handled.
     * @return the first not null element if the collection is not null and have
     *         not null value, else null.
     */
    public static <T> T getFirstNotNullValue(final Collection<T> collection) {

        if (isNotEmpty(collection)) {
            for (T element : collection) {
                if (element != null) {
                    return element;
                }
            }
        }
        return null;
    }

    /**
     * Returns true if the collection is both not null and not empty; false
     * otherwise.
     * 
     * @param collection
     *            the collection to be tested.
     * @return true if the collection is both not null and not empty; false
     *         otherwise.
     */
    public static boolean isNotEmpty(final Collection<?> collection) {

        return !isEmpty(collection);
    }

    /**
     * Returns true if the collection is null or empty; false otherwise.
     * 
     * @param collection
     *            the collection to be tested.
     * @return true if the collection is null or empty; false otherwise.
     */
    public static boolean isEmpty(final Collection<?> collection) {

        return collection == null || collection.isEmpty();
    }
}

Related

  1. getFirstItem(Collection collection)
  2. getFirstItem(Collection c)
  3. getFirstItemInCollection(Collection collection)
  4. getFirstN(Collection objects, int n)
  5. getFirstNonNull(Collection c)
  6. getFirstOrNull(final Collection collection)
  7. getFirstSortedItem( Collection items, T defaultValue)
  8. getUnionSize(Collection firstCollection, Collection secondCollection)
  9. intersect(Collection firstSet, Collection secondSet)