get First element from Collection Or Null - Java java.util

Java examples for java.util:Collection Null Element

Description

get First element from Collection Or Null

Demo Code


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

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

    public static <T> T getFirstOrNull(Collection<T> list) {

        if (isEmpty(list))
            return null;
        return list.iterator().next();
    }

    public static <T> boolean isEmpty(Collection<T> list) {

        if (list == null || list.isEmpty() || list.size() == 0) {
            return true;
        }
        return false;
    }
}

Related Tutorials