If you can require the data to be unloaded to be a List , you can get a subview of a single page using Guava :
public <T> List<T> getPage(List<T> list, int pageIndex, int pageSize) { return Lists.partition(list, pageSize).get(pageIndex); }
It does not require copying or iteration (it uses sublist views of the source list) and processes the final page, which has less than pageSize elements transparently.
For an arbitrary Iterable or Collection I would do the following:
public <T> List<T> getPage(Iterable<T> iterable, int pageIndex, int pageSize) { return Iterables.get(Iterables.partition(iterable, pageSize), pageIndex); }
By providing both of these methods, you can efficiently process objects that are known to be lists at compile time, and any other type of Iterable , as efficient as possible.
Colind
source share