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

How to use collectors of java8

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article introduces the knowledge of "how to use the collectors of java8". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Java8 provides the operation to convert the collection and then collect it. This has to mention the Collectors class, which is really powerful. Let's take a look at the example.

KeyAndValue A1 = new KeyAndValue (). SetName ("kevin"). SetValue ("lee"); KeyAndValue a2 = new KeyAndValue (). SetName ("kevin"). SetValue ("lee"); KeyAndValue a3 = new KeyAndValue (). SetName ("kevin1"). SetValue ("lee"); KeyAndValue a4 = new KeyAndValue (). SetName ("kevin1"). SetValue ("123123"); KeyAndValue a5 = new KeyAndValue (). SetName ("kevin2"). SetValue ("lee5"); KeyAndValue a6 = new KeyAndValue (). SetName ("kevin3"). SetValue ("lee8") List list = Arrays.asList (A1, a2, a3, a4, a5, a6)

Basic toList, toSet, toMap will not talk about, simple and easy to use, a look will, I will talk about a little more complicated.

CollectingAndThen: collect using collector, and then process the result of the resolution

List collect = list.stream () .collect (Collectors.collectingAndThen (Collectors.mapping (a-> a.getValue (), Collectors.toList ()), v-> {v.sort (Comparator.comparing (String::length). Reversed (); return v;})); System.out.println (collect); / / [123123, lee5, lee8, lee, lee, lee]

GroupingBy grouping operation: groups elements based on a key value

Map collect = list.stream () .collect (Collectors.groupingBy (a-> a.getName (); System.out.println (JSON.toJSONString (collect)) / / {"kevin": [{"name": "kevin", "value": "lee"}, {"name": "kevin", "value": "lee"}], "kevin2": [{"name": "kevin2", "value": "lee5"}], "kevin1": [{"name": "kevin1", "value": "lee"}, {"name": "kevin1", "value": "123123"}], "kevin3": [{"name": "kevin3"] "value": "lee8"}]} Map collect1 = list.stream () .collect (Collectors.groupingBy (KeyAndValue::getName, Collectors.mapping (b-> b.getValue (), Collectors.toSet () System.out.println (JSON.toJSONString (collect1)); / / {"kevin": ["lee"], "kevin2": ["lee5"], "kevin1": ["123123", "lee"], "kevin3": ["lee8"]} Map collect3 = list.stream (). Collect (Collectors.groupingBy (KeyAndValue::getName, Collectors.toSet ()); System.out.println (JSON.toJSONString (collect3)) / / {"kevin": [{"name": "kevin", "value": "lee"}], "kevin2": [{"name": "kevin2", "value": "lee5"}], "kevin1": [{"name": "kevin1", "value": "lee"}, {"name": "kevin1", "value": "123123"}], "kevin3": [{"name": "kevin3" "value": "lee8"}} Map collect4 = list.stream () .collect (Collectors.groupingBy (KeyAndValue::getName,TreeMap::new, Collectors.toSet () System.out.println (JSON.toJSONString (collect4)); / / {"kevin": [{"name": "kevin", "value": "lee"}], "kevin1": [{"name": "kevin1", "value": "lee"}, {"name": "kevin1", "value": "123123"}], "kevin2": [{"name": "kevin2", "value": "lee5"}], "kevin3": [{"name": "kevin3", "value": "lee8"}]}

Join operation: there are three ways to concatenate elements.

String joinResult = list.stream (). Map (KeyAndValue::getName) .cake (Collectors.joining ()); System.out.println (joinResult); / / kevinkevinkevin1kevin1kevin2kevin3String joinResult1 = list.stream () .map (KeyAndValue::getName) .conversation (Collectors.joining (","); System.out.println (joinResult1); / / kevin,kevin,kevin1,kevin1,kevin2,kevin3String joinResult2 = list.stream (). Map (KeyAndValue::getName) .requests (Collectors.joining (",")) System.out.println (joinResult2); / / (kevin,kevin,kevin1,kevin1,kevin2,kevin3)

PartitioningBy operation: elements are grouped by true and false based on assertions

Map partitioningBy = list.stream () .equals (Collectors.partitioningBy (k-> k.getName (). Equals ("kevin")); System.out.println (partitioningBy) / / {false= [KeyAndValue (name=kevin1, value=lee), KeyAndValue (name=kevin1, value=123123), KeyAndValue (name=kevin2, value=lee5), KeyAndValue (name=kevin3, value=lee8)], true= [KeyAndValue (name=kevin, value=lee), KeyAndValue (name=kevin, value=lee)} Map partitioningBy1 = list.stream () .customers (Collectors.partitioningBy (k-> k.getName (). Equals ("kevin"), Collectors.mapping (v-> v.getValue (), Collectors.toList (); System.out.println (partitioningBy1) / / {false= [lee, 123123, lee5, lee8], true= [lee, lee]}

Min/minBy, max/maxBy use the same: use a comparator to get the maximum or minimum value

Optional min = list.stream () .map (KeyAndValue::getName) .min (Comparator.comparing (v-> v.length ()); System.out.println (min.get ()); / / kevinOptional min1 = list.stream () .min (Comparator.comparing (v-> v.getValue (). Length ()); System.out.println (min1.get () / / KeyAndValue (name=kevin, value=lee) Optional min2 = list.stream (). Map (KeyAndValue::getName) .advance (Collectors.minBy (Comparator.comparing (String::length); System.out.println (min2.get ()); / / kevinOptional min3 = list.stream (). Collect (Comparator.comparing (v-> v.getValue (). Length (); System.out.println (min3.get ()); / / KeyAndValue (name=kevin, value=lee)

Mapping operation: use a function to process elements, and then use collector to collect

List mapping = list.stream (). Collect (Collectors.mapping (v-> v.getValue (). Concat ("agc"), Collectors.toList ()); System.out.println (mapping); / similar to Stream, map is performed first, followed by collectList mapping_ = list.stream (). Map (v-> v.getValue (). Concat ("agc")) .operations (Collectors.toList ()); System.out.println (mapping_); / / [leeagc, 123123agc, lee5agc, lee8agc]

ToCollection: copies elements sequentially to another collector (collection), similar to Collectors.toList ()

ArrayList collect1 = list.stream (). Collect (Collectors.toCollection (ArrayList::new)); System.out.println (collect1); / / [KeyAndValue (name=kevin, value=lee), KeyAndValue (name=kevin, value=lee), KeyAndValue (name=kevin1, value=lee), KeyAndValue (name=kevin1, value=123123), KeyAndValue (name=kevin2, value=lee5), KeyAndValue (name=kevin3, value=lee8)]

Deduplicates according to an attribute

TreeSet collect = list.stream (). Collect (Collectors.toCollection (()-> new TreeSet (Comparator.comparing (KeyAndValue::getName); System.out.println (collect); / / [KeyAndValue (name=kevin, value=lee), KeyAndValue (name=kevin1, value=lee), KeyAndValue (name=kevin2, value=lee5), KeyAndValue (name=kevin3, value=lee8)] "how to use collectors of java8". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Internet Technology

Wechat

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

12
Report