jdk8-》allMatch、anyMatch、max、min函数

allMatch函数:

检查是否匹配所有元素,只有全部符合才返回true
boolean flag = list.stream().allMatch(obj->obj.length()>5);
 
anyMatch函数
检查是否⾄少匹配⼀个元素,只要有一个符合就返回true
boolean flag = list.stream().anyMatch(obj->obj.length()>18);
 
max函数
list.stream().max(Comparator.comparingInt(Student::getAge));
 
min函数
Optional<Student> optional = list.stream().min((s1, s2)->Integer.compare(s1.getAge(),s2.getAge()));

猜你喜欢

转载自www.cnblogs.com/yuefeng123/p/12153912.html