Java9 Stream Collectors新增功能(小結(jié))
Java 9 Stream Collectors新增功能
Java 8 引入Collectors,用于累加輸入元素至可變的容器如,Map、List以及Set。本文看看Java 9 新增的兩個(gè)Collectors:Collectors.filtering 和 Collectors.flatMapping,主要用于和 Collectors.groupingBy 一起提供智能的元素集合.
Collectors.filtering方法
Collectors.filtering方法類似于Stream filter()方法,后者用于過濾輸入元素,但兩者的使用場(chǎng)景不同。Stream filter()在stream鏈接方法中使用,而Collectors.filtering方法被設(shè)計(jì)和 groupingBy一起使用。
Stream filter()首先過濾元素,然后再分組。被過濾的值被丟棄無法被追溯跟蹤。如果需要跟蹤需要先分組然后再過濾,這正是 Collectors.filtering能做的。
Collectors.filtering帶函數(shù)參數(shù)用于過濾輸入?yún)?shù),然后收集過濾元素:
@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());
}
Collectors.flatMapping方法
Collectors.flatMapping類似于Collectors.mapping 方法,但粒度更細(xì)。兩者都帶一個(gè)函數(shù)和一個(gè)收集器參數(shù)用于收集元素,但flatMapping函數(shù)接收元素流,然后通過收集器進(jìn)行累積操作。首先我們看模型類:
class Blog {
private String authorName;
private List<String> comments = new ArrayList<>();
public Blog(String authorName, String ... comment){
this.authorName = authorName;
comments.addAll(Arrays.asList(comment));
}
public String getAuthorName(){
return this.authorName;
}
public List<String> getComments(){
return comments;
}
}
Collectors.flatMapping 方法跳過中間集合,直接寫至單個(gè)有Collectors.groupingBy定義的組映射容器中:
@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映射所有分組(作者的評(píng)論)值收集的器容器中,如List。并且刪除中間集合,直接存儲(chǔ)集合至收集器的容器。
總結(jié)
本文介紹Java 9 提供Collectors新的方法。Collectors.filtering() 和 Collectors.flatMapping() ,一般和Collectors.groupingBy() 一起使用。
這些收集器也可以與collector.partitioningby()一起使用,但是僅根據(jù)條件創(chuàng)建兩個(gè)分區(qū),收集器的實(shí)際功能并沒有得到利用;因此在本教程中沒有提到。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java基于websocket協(xié)議與netty實(shí)時(shí)視頻彈幕交互實(shí)現(xiàn)
本文主要介紹了Java基于websocket協(xié)議與netty實(shí)時(shí)視頻彈幕交互實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
alibaba?seata服務(wù)端具體實(shí)現(xiàn)
seata是來處理分布式服務(wù)之間互相調(diào)用的事務(wù)問題,本文重點(diǎn)給大家介紹alibaba-seata實(shí)現(xiàn)方法,文中通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-02-02
Java利用正則表達(dá)式提取數(shù)據(jù)的方法
最近由于項(xiàng)目需求需要提取txt里的數(shù)據(jù),之前用C#實(shí)現(xiàn)過,由于最近學(xué)習(xí)了java,所以嘗試用java實(shí)現(xiàn)下,這篇文章主要介紹了Java利用正則表達(dá)式提取數(shù)據(jù)的方法,需要的朋友可以參考下,下面來一起看看吧。2016-12-12
SpringBoot中使用?ThreadLocal?進(jìn)行多線程上下文管理及注意事項(xiàng)小結(jié)
本文詳細(xì)介紹了ThreadLocal的原理、使用場(chǎng)景和示例代碼,并在SpringBoot中使用ThreadLocal保存請(qǐng)求中攜帶的用戶信息,ThreadLocal通過為每個(gè)線程維護(hù)獨(dú)立的變量副本,解決了線程安全問題,感興趣的朋友一起看看吧2025-02-02
Java開發(fā)推薦使用的JDK版本以及對(duì)比詳細(xì)分析
這篇文章詳細(xì)分析了JDK17和JDK21作為當(dāng)前推薦版本的優(yōu)缺點(diǎn),并對(duì)比了它們與JDK8和JDK11的差異,文中介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用java具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2025-04-04
spring mvc常用注解_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了spring mvc常用注解,詳細(xì)的介紹了@RequestMapping, @RequestParam, @ModelAttribute等等這樣類似的注解,有興趣的可以了解一下2017-08-08

