Filtering a list of JavaBeans with Google Guava

Do it the old-fashioned way, without Guava. (Speaking as a Guava developer.)

List<Person> filtered = Lists.newArrayList();
for(Person p : allPersons) {
   if(acceptedNames.contains(p.getName())) {
       filtered.add(p);
   }
}

You can do this with Guava, but Java isn’t Python, and trying to make it into Python is just going to perpetuate awkward and unreadable code. Guava’s functional utilities should be used sparingly, and only when they provide a concrete and measurable benefit to either lines of code or performance.

Leave a Comment