What is the best way to filter a Java Collection?

后端 未结 27 4015
故里飘歌
故里飘歌 2020-11-21 06:55

I want to filter a java.util.Collection based on a predicate.

27条回答
  •  轮回少年
    2020-11-21 07:17

    Wait for Java 8:

    List olderThan30 = 
      //Create a Stream from the personList
      personList.stream().
      //filter the element to select only those with age >= 30
      filter(p -> p.age >= 30).
      //put those filtered elements into a new List.
      collect(Collectors.toList());
    

提交回复
热议问题