Predicate_demo

参考:(15条消息) Java 8 Predicate类基本使用详解_codesWang的博客-CSDN博客

 1 package com.hmb;
 2 
 3 import java.util.Arrays;
 4 import java.util.List;
 5 import java.util.function.Predicate;
 6 
 7 public class Main {
 8     public static void main(String[] args) {
 9         List<String> strs = Arrays.asList("lkjlk", "gjgkj", "liuhliu", "ligui", "hmb");
10         Predicate<String> lenCheck = s -> s.length() > 4;
11         Predicate<String> startWith = s -> s.startsWith("l");
12         Predicate<String> equalhmb = Predicate.isEqual("hmb");
13 
14         strs.stream().filter(lenCheck).forEach(s -> System.out.print(s + " "));
15         System.out.println();
16         strs.stream().filter(startWith).forEach(s -> System.out.print(s + " "));
17         System.out.println();
18         strs.stream().filter(equalhmb).forEach(s -> System.out.print(s + " "));
19         System.out.println();
20         strs.stream().filter(lenCheck.and(startWith)).forEach(s -> System.out.print(s + " "));
21         System.out.println();
22         strs.stream().filter(lenCheck.or(startWith)).forEach(s -> System.out.print(s + " "));
23         System.out.println();
24         strs.stream().filter(lenCheck.negate()).forEach(s -> System.out.print(s + " "));
25     }
26 }

 

posted @ 2023-06-24 23:13  hemeiwolong  阅读(10)  评论(0编辑  收藏  举报