This example will show how to get the first N elements from an ArrayList using java, java 8 and guava.
Straight up Java
This snippet will truncating a ArrayList to a given number of elements in java using List.subList.
Java 8
Similar to the snippet shown how to create a stream from a function and java 8 stream intermediate operations java 8 Stream.limit is used to truncate elements in an ArrayList based on a specified size.
Google Guava
Using guava FluentIterable we will truncate an Iterable to the specified number of elements.