Java tutorial
//package com.java2s; import java.util.Iterator; import java.util.List; public class Main { public static <T> T lastOrDefault(List<T> source) { if (source == null || source.size() == 0) return null; return source.get(source.size() - 1); } public static <T> T lastOrDefault(Iterable<T> source) { if (source == null || !source.iterator().hasNext()) return null; T result = null; Iterator<T> it = source.iterator(); while (it.hasNext()) { result = it.next(); // source } return result; } }