Java tutorial
//package com.java2s; import java.util.ArrayList; import java.util.Collection; import java.util.List; public class Main { public static <T> T last(Collection<T> c) { if (isEmpty(c)) return null; if (c instanceof List) return (T) ((List<T>) c).get(c.size() - 1); List<T> a = new ArrayList<T>(c); return a.get(a.size() - 1); } 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); } }