Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use collectors of java8

Shulou Source: shulou.com Published: 2022-06-01 20:36:56 10月01日 Update

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!

Tags: Elements grouping content more knowledge processing complexity practicality power maximum minimum consistency achievement next three dilemma foundation reality attribute situation Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Linux Shulou Technology Xiaomi NVidia