return the first element from a list. - Java java.util

Java examples for java.util:List First Element

Description

return the first element from a list.

Demo Code


//package com.java2s;

import java.util.Collection;

import java.util.List;
import java.util.Map;

public class Main {
    public static void main(String[] argv) {
        List list = java.util.Arrays.asList("asdf", "java2s.com");
        System.out.println(getFirstElement(list));
    }//from   w  ww. ja v  a2s.c  om


    public static <T> T getFirstElement(List<T> list) {
        if (!isEmpty(list)) {
            return list.get(0);
        }

        return null;
    }

    public static <T> boolean isEmpty(Collection<T> collection) {
        return collection == null || collection.isEmpty();
    }
    public static <T, U> boolean isEmpty(Map<T, U> map) {
        return map == null || map.isEmpty();
    }
}

Related Tutorials