Check if Collection has At Least One Not Null Element - Java java.util

Java examples for java.util:Collection Null Element

Description

Check if Collection has At Least One Not Null Element

Demo Code


//package com.java2s;
import java.util.Collection;

public class Main {
    public static void main(String[] argv) {
        Collection collection = java.util.Arrays.asList("asdf",
                "java2s.com");
        System.out.println(hasAtLeastOneNotNullElement(collection));
    }//  w  w  w .j a  v a 2  s.  com

    public static boolean hasAtLeastOneNotNullElement(
            Collection<?> collection) {
        if (collection == null)
            return false;
        if (collection.isEmpty())
            return false;
        if (collection.contains(null))
            return collection.size() > 1;
        return collection.size() > 0;
    }
}

Related Tutorials