WHAT IS THE BEST WAY TO FILTER A JAVA COLLECTION?

Felix Pickles

Total Post:24

Points:168
Posted by  Felix Pickles
 721  View(s)
Ratings:
Rate this:
I want to filter a java.util.Collection based on a predicate.
  1. Mayank Tripathi

    Post:397

    Points:3117
    Re: What is the best way to filter a Java Collection?

    lambdaj allows to filter collections without writing loops or inner classes as in the following example:

    List<Person> beerDrinkers = select(persons, having(on(Person.class).getAge(), greaterThan(16)));

    Can you imagine something more readable? You can find it here:
    http://code.google.com/p/lambdaj/  

    With Java 8 update (2014) comes streams and lambdas, solving this problem with a simple one-liner:

    List<Person> beerDrinkers = persons.stream()
        .filter(p -> p.getAge() > 16).collect(Collectors.toList());

      Modified On Aug-07-2015 06:19:23 AM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!