This example will demonstrate how to filter a collection using java, java 8, guava and apache commons. In the snippets, we have created a NFLTeam class which we will filter all teams that have won a super bowl. A alternate example shows how to filter a list of objects by field in groovy.
An alternate /java/examples/filter-a-collection/
Setup
Straight up Java
Until Java 8, the jdk lacks a mechanism for filtering collections. The only way is to iterate over the collection and then apply criteria. In the snippet below, we will use a enhanced for loop and check if the NFLTeam has won a superbowl.
Output
Java 8
Java 8 introduced the stream api which allows you to write powerful code in just a few lines. Streams.filter is an intermediate operation that will return a stream consisting of the elements that match the specified predicate. In the snippet below, through a lambda expression we will create a java predicate that will filter all the NFLTeams that have won a superbowl.
Output
Google Guava
Guava Collections2 utility class contains a filter method that encapsulates the looping shown in the straight up java example above. In the snippet below we will create a guava predicate that will return true if the NFLTeam has won a superbowl. Then we will pass the predicate to Collections2.filter which will return the elements that satisfy the predicate.
Output
Apache Commons
Similar to guava and java predicate, apache commons has a predicate class as well. Below we will call CollectionsUtils.filter passing it a predicate that will return true if the NFLTeam has won a super bowl and it will remove the elements where it doesn't match this criteria.