Filtering a list of JavaBeans with Google Guava

前端 未结 9 759
夕颜
夕颜 2020-12-01 06:59

In a Java program, I have a list of beans that I want to filter based on a specific property.

For example, say I have a list of Person, a JavaBean, where Person has

9条回答
  •  误落风尘
    2020-12-01 07:23

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

    List 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.

提交回复
热议问题