Java8 Stream Collectors收集器使用方法解析
Collectors.toMap:
Student studentA = new Student("20190001","小明"); Student studentB = new Student("20190002","小紅"); Student studentC = new Student("20190003","小丁"); //Function.identity() 獲取這個(gè)對(duì)象本身,那么結(jié)果就是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重復(fù)的該怎么處理?這里我們假設(shè)有兩個(gè)id相同Student,如果他們id相同,在轉(zhuǎn)成Map的時(shí)候,取name大一個(gè),小的將會(huì)被丟棄。
//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)))); //可能上面比較復(fù)雜,這編寫一個(gè)命令式 //Map<String,Student> Stream.of(studentA, studentB, studentC) .collect(Collectors .toMap(Student::getId, Function.identity(), (s1, s2) -> { //這里使用compareTo 方法 s1>s2 會(huì)返回1,s1==s2 返回0 ,否則返回-1 if (((Student) s1).name.compareTo(((Student) s2).name) < -1) { return s2; } else { return s1; } }));
如果不想使用默認(rèn)的HashMap 或者 ConcurrentHashMap , 第三個(gè)重載方法還可以使用自定義的Map對(duì)象(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ū)別也僅是單線程和多線程的使用場(chǎng)景。為什么要groupingBy歸類為前后處理呢?groupingBy 是在數(shù)據(jù)收集前分組的,再將分好組的數(shù)據(jù)傳遞給下游的收集器。
這是 groupingBy最長(zhǎng)的參數(shù)的函數(shù)classifier 是分類器,mapFactory map的工廠,downstream下游的收集器,正是downstream 的存在,可以在數(shù)據(jù)傳遞個(gè)下游之前做很多的騷操作。
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ù)、負(fù)數(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ù)分為兩部分,因?yàn)閜artitioningBy分區(qū)的依據(jù)Predicate,而Predicate只會(huì)有true 和false 兩種結(jié)果,所有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()));
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Spring @Bean vs @Service注解區(qū)別
本篇文章主要介紹了Spring @Bean vs @Service注解區(qū)別,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-12-12學(xué)習(xí)Java之IO流的基礎(chǔ)概念詳解
這篇文章主要給大家介紹了Java中的IO流,我們首先要搞清楚一件事,就是為什么需要IO流這個(gè)東西,但在正式學(xué)習(xí)IO流的使用之前,小編有必要帶大家先了解一下IO流的基本概念,需要的朋友可以參考下2023-09-09Java消息隊(duì)列JMS實(shí)現(xiàn)原理解析
這篇文章主要介紹了Java消息隊(duì)列JMS實(shí)現(xiàn)原理解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03JPA @GeneratedValue 四種標(biāo)準(zhǔn)用法TABLE,SEQUENCE,IDENTITY,
這篇文章主要介紹了@GeneratedValue 四種標(biāo)準(zhǔn)用法TABLE,SEQUENCE,IDENTITY,AUTO詳解,需要的朋友可以參考下2024-03-03Maven打包所有依賴到一個(gè)可執(zhí)行jar中遇到的問題
這篇文章主要給大家介紹了關(guān)于Maven打包所有依賴到一個(gè)可執(zhí)行jar中遇到的問題,將依賴打入jar包,由于maven管理了所有的依賴,所以將項(xiàng)目的代碼和依賴打成一個(gè)包對(duì)它來(lái)說(shuō)是順理成章的功能,需要的朋友可以參考下2023-10-10解決idea中svn提交時(shí)performing vcs refresh時(shí)間很長(zhǎng)的問題
這篇文章主要介紹了解決idea中svn提交時(shí)performing vcs refresh時(shí)間很長(zhǎng)的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09mybatis 連接mysql數(shù)據(jù)庫(kù) tinyint 為boolean類型詳解
這篇文章主要介紹了mybatis 連接mysql數(shù)據(jù)庫(kù) tinyint 為boolean類型詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-11-11Java使用原型模式展現(xiàn)每日生活應(yīng)用案例詳解
這篇文章主要介紹了Java使用原型模式展現(xiàn)每日生活應(yīng)用案例,較為詳細(xì)的分析了原型模式的概念、原理及Java使用原型模式展現(xiàn)每日生活案例的相關(guān)操作步驟與注意事項(xiàng),需要的朋友可以參考下2018-05-05淺析對(duì)Java關(guān)鍵字final和static的理解
本文主要給大家談?wù)勑【帉?duì)java關(guān)鍵字final和static的理解,本文給大家介紹的較詳細(xì),需要的朋友參考參考下2017-04-04