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 Stream to remove duplicates in Java8

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

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "Java8 how to use Stream to achieve list deduplication". In daily operation, I believe many people have doubts about how Java8 uses Stream to achieve list deduplication. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubt of "how to use Stream to achieve list de-duplication". Next, please follow the editor to study!

one。 The distinct () method of Stream

Distinct () is a method provided by Stream in Java 8 that returns a stream made up of different elements in the flow. Distinct () uses the hashCode () and eqauls () methods to get different elements.

Therefore, classes that need to be deduplicated must implement the hashCode () and equals () methods. In other words, we can achieve the de-duplication of some special requirements by overriding the custom hashCode () and equals () methods.

The distinct () method is declared as follows:

Stream distinct (); 1.1 de-duplication of String lists

Because the String class has overridden the equals () and hashCode () methods, it is possible to repeat the success.

Testpublic void listDistinctByStreamDistinct () {/ / 1. For String list deduplication List stringList = new ArrayList () {{add ("A"); add ("A"); add ("B"); add ("B"); add ("C");}}; out.print ("before weight removal:"); for (String s: stringList) {out.print (s);} out.println (); stringList = stringList.stream (). Distinct (). Collect (Collectors.toList ()) Out.print ("after weight removal:"); for (String s: stringList) {out.print (s);} out.println ();}

The results are as follows:

Before de-weighting: AABBC

After removing the weight: ABC

1.2 de-duplication of the entity class list

Note: we use the @ Data annotation of the Lombok plug-in in the code, which automatically overrides the equals () and hashCode () methods.

Define an entity class * / @ Datapublic class Student {private String stuNo; private String name;} @ Testpublic void listDistinctByStreamDistinct () throws JsonProcessingException {ObjectMapper objectMapper = new ObjectMapper (); / / 1. For Student list de-weight List studentList = getStudentList (); out.print ("before weight removal:"); out.println (objectMapper.writeValueAsString (studentList)); studentList = studentList.stream (). Distinct (). Collect (Collectors.toList ()); out.print ("after weight removal:"); out.println (objectMapper.writeValueAsString (studentList));}

The results are as follows:

Before removal: [{"stuNo": "001", "name": "Tom"}, {"stuNo": "002", "name": "Mike"}, {"stuNo": "001", "name": "Tom"}]

After weight removal: [{"stuNo": "001", "name": "Tom"}, {"stuNo": "002", "name": "Mike"}]

two。 Create a new list according to an attribute of Object in List @ Test public void distinctByProperty1 () throws JsonProcessingException {/ / the first method here is to create a new list of only different elements to deduplicate ObjectMapper objectMapper = new ObjectMapper (); List studentList = getStudentList (); out.print ("before repeating:"); out.println (objectMapper.writeValueAsString (studentList)) StudentList = studentList.stream (). Distinct (). Collect (Collectors.toList ()); out.print ("distinct after weight removal:"); out.println (objectMapper.writeValueAsString (studentList)); / / here we introduce two static methods and TreeSet to achieve the effect of getting different elements / / 1. Import static java.util.stream.Collectors.collectingAndThen; / / 2. Import static java.util.stream.Collectors.toCollection StudentList = studentList.stream (). Collect (collectingAndThen (toCollection (()-> new TreeSet (Comparator.comparing (Student::getName), ArrayList::new)); out.print ("removing duplicates by name:"); out.println (objectMapper.writeValueAsString (studentList));}

The results are as follows:

Before removal: [{"stuNo": "001"," name ":" Tom "}, {" stuNo ":" 001", "name": "Tom"}, {"stuNo": "003"," name ":" Tom "}]

After distinct weight removal: [{"stuNo": "001", "name": "Tom"}, {"stuNo": "003", "name": "Tom"}]

Repeat according to the name: [{"stuNo": "001", "name": "Tom"}]

2.2 through the filter () method

We first create a method as a parameter to Stream.filter (), and its return type is Predicate. The principle is to determine whether an element can be added to the Set. The code is as follows:

Private static Predicate distinctByKey (Function keyExtractor) {Set seen = ConcurrentHashMap.newKeySet (); return t-> seen.add (keyExtractor.apply (t));

The use is as follows:

@ Test public void distinctByProperty2 () throws JsonProcessingException {/ / the second method here we use filtering to remove weight based on an attribute of the object ObjectMapper objectMapper = new ObjectMapper (); List studentList = getStudentList (); out.print ("before weight removal:"); out.println (objectMapper.writeValueAsString (studentList)); studentList = studentList.stream (). Distinct (). Collect (Collectors.toList ()); out.print ("distinct after weight removal:") Out.println (objectMapper.writeValueAsString (studentList)); / / here we use the distinctByKey () method as the parameter of filter () to filter out the elements studentList = studentList.stream (). Filter (distinctByKey (Student::getName)) .duplicate (Collectors.toList ()); out.print ("remove duplicates by name:"); out.println (objectMapper.writeValueAsString (studentList));}

The results are as follows:

Before removal: [{"stuNo": "001", "name": "Tom"}, {"stuNo": "001", "name": "Tom"}, {"stuNo": "003", "name": "Tom"}]

After distinct weight removal: [{"stuNo": "001", "name": "Tom"}, {"stuNo": "003", "name": "Tom"}]

Repeat according to the name: [{"stuNo": "001", "name": "Tom"}]

At this point, the study on "how Java8 uses Stream to achieve list de-duplication" is over. I hope to be able to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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