Provides the first item of the given Iterable. - Java Collection Framework

Java examples for Collection Framework:Iterable

Description

Provides the first item of the given Iterable.

Demo Code


//package com.java2s;

import java.util.Iterator;

public class Main {
    /**//from   w  w w.j a  va2 s  . c om
     * Provides the first item of the given list.
     *
     * @param list the list. May be <code>null</code>.
     * @return the first list item or <code>null</code> if the list was empty or <code>null</code>.
     */
    public static <T> T firstItem(Iterable<T> list) {
        if (list != null) {
            Iterator<T> i = list.iterator();
            if (i.hasNext()) {
                return i.next();
            }
        }

        return null;
    }
}

Related Tutorials