List.subList(0, 20)
will throw an exception if your list contains less than 20 elements.
With Java 8:
You can use Stream.limit () :
List<Staff> second = staffs.stream().limit(20).collect(Collectors.toList());
With Java 7 or lower:
You can use Guava Iterables.limit () to get all available elements, but no more than 20:
List<Staff> second = Lists.newArrayList(Iterables.limit(staffs, 20));
Arend v. Reinersdorff
source share