Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the new features of StreamCollectors in Java9

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces what are the new features of StreamCollectors in Java9, which can be used for reference by interested friends. I hope you can learn a lot after reading this article.

What's New in Java 9 Stream Collectors

Java 8 introduces Collectors to accumulate input elements to mutable containers such as Map, List, and Set. This article looks at the two new Collectors:Collectors.filtering and Collectors.flatMapping in Java 9, which are mainly used to provide an intelligent collection of elements together with Collectors.groupingBy.

Collectors.filtering method

The Collectors.filtering method is similar to the Stream filter () method, which is used to filter input elements, but the usage scenarios are different. Stream filter () is used in the stream link method, while the Collectors.filtering method is designed to be used in conjunction with groupingBy.

Stream filter () first filters the elements and then groups them. Filtered values are discarded and cannot be traced back. If tracing needs to be grouped and then filtered, this is exactly what Collectors.filtering can do.

Collectors.filtering takes function parameters to filter input parameters, and then collects filter elements:

@ Testpublic void givenList_whenSatifyPredicate_thenMapValueWithOccurences () {List numbers = List.of (1,2,3,5,5); Map result = numbers.stream () .filter (val-> val > 3) .filter (Collectors.groupingBy (I-> I, Collectors.counting ()); assertEquals (1, result.size ()); result = numbers.stream () .cake (Collectors.groupingBy (I-> I, Collectors.filtering (val-> val > 3, Collectors.counting (); assertEquals (4, result.size ()) }

Collectors.flatMapping method

Collectors.flatMapping is similar to the Collectors.mapping method, but with finer granularity. Both take a function and a collector parameter for collecting elements, but the flatMapping function receives a stream of elements and then accumulates through the collector. First, let's look at the model class:

Class Blog {private String authorName;private List 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 getComments () {return comments;}}

The Collectors.flatMapping method skips the intermediate collection and writes directly to a single group mapping container defined by Collectors.groupingBy:

@ Testpublic void givenListOfBlogs_whenAuthorName_thenMapAuthorWithComments () {Blog blog1 = new Blog ("1", "Nice", "Very Nice"); Blog blog2 = new Blog ("2", "Disappointing", "Ok", "Could be better"); List blogs = List.of (blog1, blog2); Map authorComments1 = blogs.stream (). 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 authorComments2 = blogs.stream (). 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 maps all grouping (author's comments) values to the collection of containers, such as List. And delete the intermediate collection and store the collection directly to the container of the collector.

Thank you for reading this article carefully. I hope the article "what are the new functions of StreamCollectors in Java9" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report