Java tutorial
//package com.java2s; import java.util.*; public class Main { /** * Returns the first item in the collection or null if the collection is empty */ public static <X> X getFirstItem(Collection<X> c) { X o; if (c == null || c.isEmpty()) { o = null; } else if (c instanceof List) { o = ((List<X>) c).get(0); } else { o = c.iterator().next(); } return o; } }