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));

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