What is the best way to filter a Java Collection?

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

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

27条回答
  •  情书的邮戳
    2020-11-21 07:36

    In Java 8, You can directly use this filter method and then do that.

     List lines = Arrays.asList("java", "pramod", "example");
    
     List result = lines.stream()              
             .filter(line -> !"pramod".equals(line))     
             .collect(Collectors.toList());              
    
     result.forEach(System.out::println); 
    

提交回复
热议问题