Java tutorial
//package com.java2s; //it under the terms of the GNU Affero General Public License as published by import java.util.Iterator; public class Main { /** * @deprecated Strongly consider if you want the first element or rather only expect * one element. In the latter case, use {@link #getUnique(Iterable)} or * {@link #getSingleIfExist(Iterable)}. */ public static <T> T getFirst(Iterable<T> iterable) { return getFirst(iterable, null); } public static <T> T getFirst(Iterable<? extends T> iterable, T def) { final Iterator<? extends T> iter = iterable.iterator(); return iter.hasNext() ? iter.next() : def; } }