Java tutorial
//package com.java2s; import java.util.Collection; import java.util.List; public class Main { /** * Return the first item from a collection using the most efficient * method possible. Returns null for an empty collection. * * @param c The Collection. * @return the first element of a Collection. */ public static Object getFirstItemOrNull(Collection c) { if (c.size() == 0) { return null; } return getFirstItem(c); } /** * Return the first item from a collection using the most efficient * method possible. * * @param c The Collection. * @return the first element of a Collection. * @throws java.util.NoSuchElementException if the collection is empty. */ public static Object getFirstItem(Collection c) { if (c instanceof List) { return ((List) c).get(0); } return c.iterator().next(); } }