Java9中新增的Collector收集器
前言:
Java 8中添加了收集器Collectors
,這有助于將輸入元素累積到諸如Map
、List
和Set
等可變?nèi)萜髦小?/p>
在本文中,我們將探討Java 9中添加的兩個新收集器:Collectors.filtering
和 Collectors.flatMapping
。flatMapping
與收集器結(jié)合使用。通過提供智能元素集合進行分組。
Filtering Collector
Collectors.filtering
類似于流過濾器Stream filter()
;它用于過濾輸入元素,但用于不同的場景。流的過濾器在流鏈中使用,而過濾是一個收集器,設(shè)計用于與groupingBy
一起使用。
使用流的過濾器,首先過濾值,然后對其進行分組。這樣,被過濾掉的值就消失了,沒有任何痕跡。如果我們需要一個跟蹤,那么我們需要先分組,然后應(yīng)用過濾,這實際上是Collectors.filtering
做的。
Collectors.filtering
功能用于過濾輸入元素,收集器用于收集過濾后的元素:
@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
類似于Collectors.mapping
。但有一個更細粒度的目標(biāo)。兩個采集器都接受一個函數(shù)和一個采集器,其中收集元素,但flatMapping函數(shù)接受元素流,然后由采集器累積。
讓我們看看下面的模型類:
class Blog { private String authorName; private List<String> comments; // constructor and getters }
Collectors.flatMapping
允許我們跳過中間集合,直接寫入映射到收集器定義的組的單個容器。
分組方式:
@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刪除,因為它提供了要映射到收集器容器的注釋列表的直接流。
結(jié)論
本文說明了Java9中引入的新收集器的使用,即Collectors.filtering()
和 Collectors.flatMapping()
與Collectors.groupingBy()
結(jié)合使用。
這些收集器也可以與Collectors.partitioningBy()
一起使用。但它只根據(jù)條件創(chuàng)建兩個分區(qū),并且沒有利用收集器的實際功能;
本文中代碼片段的完整源代碼可在GitHub上獲得:
到此這篇關(guān)于Java9中新增的Collector收集器的文章就介紹到這了,更多相關(guān)Java9 Collector內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring rest接口中的LocalDateTime日期類型轉(zhuǎn)時間戳
這篇文章主要介紹了Spring rest接口中的LocalDateTime日期類型轉(zhuǎn)時間戳的方法,Java程序中一般將日期類型定義為LocalDateTime,數(shù)據(jù)庫中保存的時間是0時區(qū)的時間2023-03-03JSON.parseObject和JSON.toJSONString實例詳解
這篇文章主要為大家詳細介紹了JSON.parseObject和JSON.toJSONString實例,具有一定的參考價值,感興趣的朋友可以參考一下2018-06-06maven項目后出現(xiàn)‘parent.relativePath’ of POM錯誤時的解決方法
在Springboot項目啟動時,項目報錯‘parent.relativePath’ of POM問題,項目無法正常啟動,本文就來介紹一下解決方法,感興趣的可以了解一下2023-10-10如何在java文件中設(shè)置文字顏色:setTextColor()
這篇文章主要介紹了如何在java文件中設(shè)置文字顏色:setTextColor(),文末補充介紹了在java代碼中設(shè)置字體顏色方法總結(jié),結(jié)合實例代碼介紹的非常詳細,需要的朋友可以參考下2023-09-09