欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來(lái)到入門(mén)教程網(wǎng)!

Java

當(dāng)前位置:主頁(yè) > 軟件編程 > Java >

Java9 Stream Collectors新增功能(小結(jié))

來(lái)源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:Java|點(diǎn)擊: 次

Java 9 Stream Collectors新增功能

Java 8 引入Collectors,用于累加輸入元素至可變的容器如,Map、List以及Set。本文看看Java 9 新增的兩個(gè)Collectors:Collectors.filtering 和 Collectors.flatMapping,主要用于和 Collectors.groupingBy 一起提供智能的元素集合.

Collectors.filtering方法

Collectors.filtering方法類(lèi)似于Stream filter()方法,后者用于過(guò)濾輸入元素,但兩者的使用場(chǎng)景不同。Stream filter()在stream鏈接方法中使用,而Collectors.filtering方法被設(shè)計(jì)和 groupingBy一起使用。

Stream filter()首先過(guò)濾元素,然后再分組。被過(guò)濾的值被丟棄無(wú)法被追溯跟蹤。如果需要跟蹤需要先分組然后再過(guò)濾,這正是 Collectors.filtering能做的。

Collectors.filtering帶函數(shù)參數(shù)用于過(guò)濾輸入?yún)?shù),然后收集過(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());
}

Collectors.flatMapping方法

Collectors.flatMapping類(lèi)似于Collectors.mapping 方法,但粒度更細(xì)。兩者都帶一個(gè)函數(shù)和一個(gè)收集器參數(shù)用于收集元素,但flatMapping函數(shù)接收元素流,然后通過(guò)收集器進(jìn)行累積操作。首先我們看模型類(lèi):

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 方法跳過(guò)中間集合,直接寫(xiě)至單個(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í)際功能并沒(méi)有得到利用;因此在本教程中沒(méi)有提到。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。

上一篇:java中的String定義的字面量最大長(zhǎng)度是多少

欄    目:Java

下一篇:簡(jiǎn)單了解Spring中BeanFactory與FactoryBean的區(qū)別

本文標(biāo)題:Java9 Stream Collectors新增功能(小結(jié))

本文地址:http://mengdiqiu.com.cn/a1/Java/8878.html

網(wǎng)頁(yè)制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語(yǔ)言數(shù)據(jù)庫(kù)服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有