Java8 Stream Collectors收集器使用方法解析
Collectors.toMap:
Student studentA = new Student("20190001","小明"); Student studentB = new Student("20190002","小紅"); Student studentC = new Student("20190003","小丁"); //Function.identity() 獲取這個對象本身,那么結果就是Map<String,Student> 即 id->student //串行收集 Stream.of(studentA,studentB,studentC) .collect(Collectors.toMap(Student::getId,Function.identity())); //并發(fā)收集 Stream.of(studentA,studentB,studentC) .parallel() .collect(Collectors.toConcurrentMap(Student::getId,Function.identity())); //================================================================================ //Map<String,String> 即 id->name //串行收集 Stream.of(studentA,studentB,studentC) .collect(Collectors.toMap(Student::getId,Student::getName)); //并發(fā)收集 Stream.of(studentA,studentB,studentC) .parallel() .collect(Collectors.toConcurrentMap(Student::getId,Student::getName));
那么如果key重復的該怎么處理?這里我們假設有兩個id相同Student,如果他們id相同,在轉成Map的時候,取name大一個,小的將會被丟棄。
//Map<String,Student> //maxby ==sordBy 倒序 minBy or .maxBy(Comparator.comparing(User::getName).reversed()))); Stream.of(studentA, studentB, studentC) .collect(Collectors .toMap(Student::getId, Function.identity(), BinaryOperator .maxBy(Comparator.comparing(Student::getName)))); //可能上面比較復雜,這編寫一個命令式 //Map<String,Student> Stream.of(studentA, studentB, studentC) .collect(Collectors .toMap(Student::getId, Function.identity(), (s1, s2) -> { //這里使用compareTo 方法 s1>s2 會返回1,s1==s2 返回0 ,否則返回-1 if (((Student) s1).name.compareTo(((Student) s2).name) < -1) { return s2; } else { return s1; } }));
如果不想使用默認的HashMap 或者 ConcurrentHashMap , 第三個重載方法還可以使用自定義的Map對象(Map工廠)。
//自定義LinkedHashMap //Map<String,Student> Stream.of(studentA, studentB, studentC) .collect(Collectors .toMap(Student::getId, Function.identity(), BinaryOperator .maxBy(Comparator.comparing(Student::getName)), LinkedHashMap::new));
Collectors.groupingBy()和Collectors.groupingByConcurrent(),這兩者區(qū)別也僅是單線程和多線程的使用場景。為什么要groupingBy歸類為前后處理呢?groupingBy 是在數(shù)據(jù)收集前分組的,再將分好組的數(shù)據(jù)傳遞給下游的收集器。
這是 groupingBy最長的參數(shù)的函數(shù)classifier 是分類器,mapFactory map的工廠,downstream下游的收集器,正是downstream 的存在,可以在數(shù)據(jù)傳遞個下游之前做很多的騷操作。
public static <T, K, D, A, M extends Map<K, D>> Collector<T, ?, M> groupingBy(Function<? super T, ? extends K> classifier, Supplier<M> mapFactory, Collector<? super T, A, D> downstream)
示例:這里將一組數(shù)整型數(shù)分為正數(shù)、負數(shù)、零,groupingByConcurrent()的參數(shù)也是跟它一樣的就不舉例了。
//Map<String,List<Integer>> Stream.of(-6, -7, -8, -9, 1, 2, 3, 4, 5, 6) .collect(Collectors.groupingBy(integer -> { if (integer < 0) { return "小于"; } else if (integer == 0) { return "等于"; } else { return "大于"; } })); //Map<String,Set<Integer>> //自定義下游收集器 Stream.of(-6, -7, -8, -9, 1, 2, 3, 4, 5, 6) .collect(Collectors.groupingBy(integer -> { if (integer < 0) { return "小于"; } else if (integer == 0) { return "等于"; } else { return "大于"; } },Collectors.toSet())); //Map<String,Set<Integer>> //自定義map容器 和 下游收集器 Stream.of(-6, -7, -8, -9, 1, 2, 3, 4, 5, 6) .collect(Collectors.groupingBy(integer -> { if (integer < 0) { return "小于"; } else if (integer == 0) { return "等于"; } else { return "大于"; } },LinkedHashMap::new,Collectors.toSet()));
Collectors.partitioningBy()
字面意思話就叫分區(qū)好了,但是partitioningBy最多只能將數(shù)據(jù)分為兩部分,因為partitioningBy分區(qū)的依據(jù)Predicate,而Predicate只會有true 和false 兩種結果,所有partitioningBy最多只能將數(shù)據(jù)分為兩組。partitioningBy除了分類器與groupingBy 不一樣外,其他的參數(shù)都相同。
示例:
//Map<Boolean,List<Integer>> Stream.of(0,1,0,1) .collect(Collectors.partitioningBy(integer -> integer==0)); //Map<Boolean,Set<Integer>> //自定義下游收集器 Stream.of(0,1,0,1) .collect(Collectors.partitioningBy(integer -> integer==0,Collectors.toSet()));
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Spring @Bean vs @Service注解區(qū)別
本篇文章主要介紹了Spring @Bean vs @Service注解區(qū)別,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12JPA @GeneratedValue 四種標準用法TABLE,SEQUENCE,IDENTITY,
這篇文章主要介紹了@GeneratedValue 四種標準用法TABLE,SEQUENCE,IDENTITY,AUTO詳解,需要的朋友可以參考下2024-03-03Maven打包所有依賴到一個可執(zhí)行jar中遇到的問題
這篇文章主要給大家介紹了關于Maven打包所有依賴到一個可執(zhí)行jar中遇到的問題,將依賴打入jar包,由于maven管理了所有的依賴,所以將項目的代碼和依賴打成一個包對它來說是順理成章的功能,需要的朋友可以參考下2023-10-10解決idea中svn提交時performing vcs refresh時間很長的問題
這篇文章主要介紹了解決idea中svn提交時performing vcs refresh時間很長的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09mybatis 連接mysql數(shù)據(jù)庫 tinyint 為boolean類型詳解
這篇文章主要介紹了mybatis 連接mysql數(shù)據(jù)庫 tinyint 為boolean類型詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11