Java tutorial
//package com.java2s; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import java.util.List; public class Main { public static <T> T first(Collection<T> c) { if (isEmpty(c)) return null; if (c instanceof List) return (T) ((List<T>) c).get(0); Iterator<T> iter = c.iterator(); if (iter.hasNext()) { return iter.next(); } return null; } public static boolean isEmpty(Collection<? extends Object> c) { return c == null || c.size() == 0; } public static boolean isEmpty(Object[] objs) { return objs == null || objs.length == 0; } public static <T> T get(Collection<T> c, int index) { if (isEmpty(c)) return null; if (index < 0 || index >= c.size()) return null; if (c instanceof List) return (T) ((List<T>) c).get(index); List<? extends T> a = new ArrayList<T>(c); return a.get(index); } }