We can use CollectionUtils of Apache for this purpose. We will
have to create a Predicate that will define the condition for our
filter. Then we can apply this Predicate in filter() method.
Sample code is:
In this example we filter any names that are less than 5 characters
long.
List
List
shortNamesList.addAll( namesList );
CollectionUtils.filter( shortNamesList, new Predicate(){
public boolean evaluate( Object input ) {
return ((String) input).length() < 5;
}
} );
We can also use Google Guava library for this.
In Java 8, we can use Predicate to filter a Collection through
Stream.