Wednesday, 18 November 2015

How to filter / sort a Java collection

The best way to filter a Java collection is to use Java 8. Java streams and lambdas can be used to filter a collection as below:

List passedStudents = students.stream().filter(p -> p.getMark() > 50).collect(Collectors.toList());

Before Java 8 - Using Comparator Interface:

Before Java 8 - Using Comparable Interface:

Collections.sort(animals);

 

No comments:

Post a Comment