import java.util.Comparator; import java.util.List; import java.util.stream.Collector; import org.junit.Test; import com.google.common.collect.Comparators; import com.google.common.collect.Lists; public class TestComparator { @Test public void testTopN() { final List<Integer> numbers = Lists.newArrayList(1, 3, 8, 2, 6, 4, 7, 5, 9, 0); final Collector<Integer, ?, List<Integer>> collector = Comparators.greatest(5, Comparator.<Integer>naturalOrder()); final List<Integer> top = numbers.stream().collect(collector); System.out.println(top); } }