Java9中新增的Collector收集器
前言:
Java 8中添加了收集器Collectors,這有助于將輸入元素累積到諸如Map、List和Set等可變?nèi)萜髦小?/p>
在本文中,我們將探討Java 9中添加的兩個(gè)新收集器:Collectors.filtering 和 Collectors.flatMapping。flatMapping與收集器結(jié)合使用。通過(guò)提供智能元素集合進(jìn)行分組。
Filtering Collector
Collectors.filtering類(lèi)似于流過(guò)濾器Stream filter();它用于過(guò)濾輸入元素,但用于不同的場(chǎng)景。流的過(guò)濾器在流鏈中使用,而過(guò)濾是一個(gè)收集器,設(shè)計(jì)用于與groupingBy一起使用。
使用流的過(guò)濾器,首先過(guò)濾值,然后對(duì)其進(jìn)行分組。這樣,被過(guò)濾掉的值就消失了,沒(méi)有任何痕跡。如果我們需要一個(gè)跟蹤,那么我們需要先分組,然后應(yīng)用過(guò)濾,這實(shí)際上是Collectors.filtering做的。
Collectors.filtering功能用于過(guò)濾輸入元素,收集器用于收集過(guò)濾后的元素:
@Test
public void givenList_whenSatifyPredicate_thenMapValueWithOccurences() {
List<Integer> numbers = List.of(1, 2, 3, 5, 5);
Map<Integer, Long> result = numbers.stream()
.filter(val -> val > 3)
.collect(Collectors.groupingBy(i -> i, Collectors.counting()));
assertEquals(1, result.size());
result = numbers.stream()
.collect(Collectors.groupingBy(i -> i,
Collectors.filtering(val -> val > 3, Collectors.counting())));
assertEquals(4, result.size());
}FlatMapping Collector
Collectors.flatMapping類(lèi)似于Collectors.mapping。但有一個(gè)更細(xì)粒度的目標(biāo)。兩個(gè)采集器都接受一個(gè)函數(shù)和一個(gè)采集器,其中收集元素,但flatMapping函數(shù)接受元素流,然后由采集器累積。
讓我們看看下面的模型類(lèi):
class Blog {
private String authorName;
private List<String> comments;
// constructor and getters
}Collectors.flatMapping允許我們跳過(guò)中間集合,直接寫(xiě)入映射到收集器定義的組的單個(gè)容器。
分組方式:
@Test
public void givenListOfBlogs_whenAuthorName_thenMapAuthorWithComments() {
Blog blog1 = new Blog("1", "Nice", "Very Nice");
Blog blog2 = new Blog("2", "Disappointing", "Ok", "Could be better");
List<Blog> blogs = List.of(blog1, blog2);
Map<String, List<List<String>>> authorComments1 = blogs.stream()
.collect(Collectors.groupingBy(Blog::getAuthorName,
Collectors.mapping(Blog::getComments, Collectors.toList())));
assertEquals(2, authorComments1.size());
assertEquals(2, authorComments1.get("1").get(0).size());
assertEquals(3, authorComments1.get("2").get(0).size());
Map<String, List<String>> authorComments2 = blogs.stream()
.collect(Collectors.groupingBy(Blog::getAuthorName,
Collectors.flatMapping(blog -> blog.getComments().stream(),
Collectors.toList())));
assertEquals(2, authorComments2.size());
assertEquals(2, authorComments2.get("1").size());
assertEquals(3, authorComments2.get("2").size());
}Collectors.mapping將所有分組作者的注釋映射到收集器的容器(即列表),而此中間集合將使用flatMapping刪除,因?yàn)樗峁┝艘成涞绞占魅萜鞯淖⑨屃斜淼闹苯恿鳌?/p>
結(jié)論
本文說(shuō)明了Java9中引入的新收集器的使用,即Collectors.filtering() 和 Collectors.flatMapping()與Collectors.groupingBy()結(jié)合使用。
這些收集器也可以與Collectors.partitioningBy()一起使用。但它只根據(jù)條件創(chuàng)建兩個(gè)分區(qū),并且沒(méi)有利用收集器的實(shí)際功能;
本文中代碼片段的完整源代碼可在GitHub上獲得:
到此這篇關(guān)于Java9中新增的Collector收集器的文章就介紹到這了,更多相關(guān)Java9 Collector內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot整合Lucene實(shí)現(xiàn)全文檢索的詳細(xì)步驟
全文搜索(Full-Text?Search)是指對(duì)大規(guī)模存儲(chǔ)在計(jì)算機(jī)系統(tǒng)中的文本數(shù)據(jù)進(jìn)行檢索和匹配的技術(shù),它允許用戶輸入關(guān)鍵字,然后從海量的文本數(shù)據(jù)中快速找到相關(guān)的信息,本文介紹了SpringBoot整合Lucene實(shí)現(xiàn)全文檢索的詳細(xì)步驟,需要的朋友可以參考下2024-03-03
idea2020.2卡死在reading maven projects
這篇文章主要介紹了idea2020.2卡死在reading maven projects,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
Java實(shí)現(xiàn)八種排序算法詳細(xì)代碼舉例
排序問(wèn)題一直是程序員工作與面試的重點(diǎn),今天特意整理研究下與大家共勉!這篇文章主要介紹了Java實(shí)現(xiàn)八種排序算法的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-10-10
SpringBoot2.6.x默認(rèn)禁用循環(huán)依賴(lài)后的問(wèn)題解決
由于SpringBoot從底層逐漸引導(dǎo)開(kāi)發(fā)者書(shū)寫(xiě)規(guī)范的代碼,同時(shí)也是個(gè)憂傷的消息,循環(huán)依賴(lài)的應(yīng)用場(chǎng)景實(shí)在是太廣泛了,所以SpringBoot 2.6.x不推薦使用循環(huán)依賴(lài),本文給大家說(shuō)下SpringBoot2.6.x默認(rèn)禁用循環(huán)依賴(lài)后的應(yīng)對(duì)策略,感興趣的朋友一起看看吧2022-02-02
SpringBoot之如何搭建SpringBoot+Maven項(xiàng)目
這篇文章主要介紹了SpringBoot之如何搭建SpringBoot+Maven項(xiàng)目問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
Spring框架配置java web實(shí)現(xiàn)實(shí)例化
這篇文章主要介紹了Spring框架配置java web實(shí)現(xiàn)實(shí)例化,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04

