Returns the first element of the list, null if the list is null or empty. - Java java.util

Java examples for java.util:List First Element

Description

Returns the first element of the list, null if the list is null or empty.

Demo Code


//package com.java2s;

import java.util.List;

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

    /**
     * Returns the first element of the list, null if the list is null or empty.
     * 
     * @param <T>
     * @param list
     * @return
     */
    public static <T> T first(List<T> list) {
        if (list == null || list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }
}

Related Tutorials