What is the best way to filter a Java Collection?

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

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

27条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-21 07:30

    Using java 8, specifically lambda expression, you can do it simply like the below example:

    myProducts.stream().filter(prod -> prod.price>10).collect(Collectors.toList())
    

    where for each product inside myProducts collection, if prod.price>10, then add this product to the new filtered list.

提交回复
热议问题