lambda 表达式去重、查找元素、分组、过滤

 1. lambda 表达式根据对象某个属性进行去重
 List<ReparationOperation> insuranceReparationsMoneyList = reparationOperationsList.stream().collect(
                    Collectors.collectingAndThen(
                            Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(ReparationOperation::getInsuranceReparationsMoney))), ArrayList::new));


ReparationOperation这个list的streamAPI的聚合操作collect可以让我们只关注结果,而collect方法里的collectingAndThen又是属 于                java.util.stream.Collector,collectingAndThen操作的解释是:先执行前面的操作,然后执行第二部操作后输出结果,这里我们执行的第一步操作就是Collectors.toCollection(()  -> new TreeSet<>(Comparator.comparing(ReparationOperation::getInsuranceReparationsMoney))),第二步就是将他输出为一个新的ArrayList。

第一步操作里又是用到Collectors接口,这次用的是toCollection方法,就是将方法里的函数或者参数转化为一个collection集合,这里,我们是将Comparator.comparing(ReparationOperation::getInsuranceReparationsMoney)转化为一个collection,这个collection是一个TreeSet。也就是有序的。因为我们需要去掉重复的值,这个set可以做到,而我们又要保持转化出来的collection依旧有序,所以使用的是一个TreeSet。

Comparator.comparing(Person::getName)这里呢,又用到了java.util.Comparator接口,这个接口倒是挺常用的。使用的是他的comparing方法,也就是比较参数的值是否相同,里面用到的是java8的新特性lambda表达式, :: 其实和.没太大区别,举个例子,最常用的System.out.println() 可以表达为System.out::println,可以达到一样的效果

2.元素查找与过滤

filter:List<Student> whuStudents = students.stream()
                                    .filter(student -> "武汉大学".equals(student.getSchool()))
                                    .collect(Collectors.toList());
distinct:List<Integer> evens = nums.stream()
                        .filter(num -> num % 2 == 0).distinct()
                        .collect(Collectors.toList());
limit:    List<Student> civilStudents = students.stream()
                                    .filter(student -> "土木工程".equals(student.getMajor())).limit(2)
                                    .collect(Collectors.toList());

allMatch用于检测是否全部都满足指定的参数行为,如果全部满足则返回true,例如我们希望检测是否所有的学生都已满18周岁,那么可以实现为:
    boolean isAdult = students.stream().allMatch(student -> student.getAge() >= 18);                                
anyMatch则是检测是否存在一个或多个满足指定的参数行为,如果满足则返回true,例如我们希望检测是否有来自武汉大学的学生,那么可以实现为:
    boolean hasWhu = students.stream().anyMatch(student -> "武汉大学".equals(student.getSchool()));
noneMatch用于检测是否不存在满足指定行为的元素,如果不存在则返回true,例如我们希望检测是否不存在专业为计算机科学的学生,可以实现如下:
    boolean noneCs = students.stream().noneMatch(student -> "计算机科学".equals(student.getMajor()));
findFirst用于返回满足条件的第一个元素,比如我们希望选出专业为土木工程的排在第一个学生,那么可以实现如下:
    Optional<Student> optStu = students.stream().filter(student -> "土木工程".equals(student.getMajor())).findFirst();
findAny相对于findFirst的区别在于,findAny不一定返回第一个,而是返回任意一个,比如我们希望返回任意一个专业为土木工程的学生,可以实现如下:
     Optional<Student> optStu = students.stream().filter(student -> "土木工程".equals(student.getMajor())).findAny(); 
3.lambda分组操作:
     Map<String, List<Student>> groups = students.stream().collect(Collectors.groupingBy(Student::getSchool));
     
     List<CarProduct> distinctList = carProductList.stream().collect(
                Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getCompanyId()))), ArrayList::new));

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章