Java tutorial
//package com.java2s; import java.util.Iterator; import java.util.SortedSet; public class Main { public static <T> T findLastItem(SortedSet<T> queryResult, int lastItemPosition) { if (queryResult == null || queryResult.isEmpty()) throw new IllegalArgumentException("Provided result set is 'null' or empty."); Iterator<T> resultSetIterator = queryResult.iterator(); int nbItemInList = 0; T lastItem = null; while (resultSetIterator.hasNext() && nbItemInList++ < lastItemPosition) lastItem = resultSetIterator.next(); return lastItem; } }