stream流:allMatch,anyMatch的用法
Student stu1 = new Student();
stu1.setId(11);
stu1.setName("luck911");
stu1.setScore(list);
stu1.setStu_Class(map);
Student stu2 = Student.builder().id(12).name("luck912").score(list).Stu_Class(map).build();
List<Student> stuList = new ArrayList<Student>();
stuList.add(stu1);
stuList.add(stu2);
// list(Score)中如果包含“11”,返回true,否则返回false
Boolean s7 = stuList.stream().allMatch(a7 -> a7.getScore().contains("12"));
// list(Name)中如果每一个值都是“luck912”,返回true,否则返回false
Boolean s8 = stuList.stream().allMatch(a8 -> StringUtils.equals(a8.getName(), "luck912"));
// list(Name)中如果每一个值都不是“luck912”,返回true,否则返回false
Boolean s9 = stuList.stream().allMatch(a9 -> !StringUtils.equals(a9.getName(), "kkkkk"));
// list(Name)中如果有一个值是“luck912”,返回true,否则返回false
Boolean s10 = stuList.stream().anyMatch(a10 -> StringUtils.equals(a10.getName(), "luck912"));
System.out.println("s7 : " + s7);
System.out.println("s8 : " + s8);
System.out.println("s9 : " + s9);
System.out.println("s10 : " + s10);
运行结果为:
s7 : true
s8 : false
s9 : true
s10 : true