In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the relevant knowledge of "how to achieve List collection de-duplication in java". 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!
Foreword:
List de-reduplication refers to the process of removing repeating elements from the List. This topic examines the ability to understand and flexibly apply the new features in List iterators, Set collections, and JDK 8.
There are three ways to realize List de-repetition:
The custom method is used to remove duplicates, and determine whether there are multiple elements through a loop. If there are multiple elements, delete the duplicates, and loop the whole collection to get a List without repeating elements.
Use Set set to remove duplicates, and make use of the feature of Set collection itself to realize the deduplication of List.
Use the deduplication feature of Stream streams in JDK 8.
1. Custom deduplication
There are two ways to implement custom deduplication. First, we can create a new set, loop the original set to determine whether the element of the loop already exists in the new set, and insert it if it does not exist, otherwise ignore it, so the loop is finished. The resulting new set is a set without repeating elements.
The specific implementation code is as follows:
Import lombok.Data;import java.util.ArrayList;import java.util.List;public class DistinctExample {public static void main (String [] args) {/ / create and assign List List list = new ArrayList (); list.add (new Person ("Li Si", "123456", 20); list.add ("Zhang San", "123456", 18)) List.add (new Person ("Wang Wu", "123456", 22); list.add (new Person ("Zhang San", "123456", 18)); / / deduplication operation List newList = new ArrayList (list.size ()) List.forEach (I-> {if (! newList.contains (I)) {/ / insert newList.add (I) if the new collection does not exist;}}); / / print the collection newList.forEach (p-> System.out.println (p));}} @ Dataclass Person {private String name; private String password Private int age; public Person (String name, String password, int age) {this.name = name; this.password = password; this.age = age;}}
The result of the above program execution is shown in the following figure:
The second method to realize the custom deduplication function is to loop through the iterator and determine whether the first occurrence position (indexOf) of the current element is equal to the last occurrence position (lastIndexOf). If not, it means that the element is a repeating element, and you can delete the current element. In this way, you can get a collection without repeating elements after the loop.
The implementation code is as follows:
Import lombok.Data;import java.util.ArrayList;import java.util.Iterator;import java.util.List;public class DistinctExample {public static void main (String [] args) {/ / create and assign List List list = new ArrayList (); list.add (new Person ("Li Si", "123456", 20); list.add ("Zhang San", "123456", 18)) List.add (new Person ("Wang Wu", "123456", 22); list.add (new Person ("Zhang San", "123456", 18)); / / deduplication operation Iterator iterator = list.iterator (); while (iterator.hasNext ()) {/ / get the value of the loop Person item = iterator.next () / / if there are two identical values if (list.indexOf (item)! = list.lastIndexOf (item)) {/ / remove the same value iterator.remove ();}} / / print collection information list.forEach (p-> System.out.println (p)) } @ Dataclass Person {private String name; private String password; private int age; public Person (String name, String password, int age) {this.name = name; this.password = password; this.age = age;}}
The result of the above program execution is shown in the following figure:
two。 Using Set set to remove duplicates
Set collections are inherently deduplicated. When you create a Set collection, you can pass a List collection, so that you can transfer and remove data. The specific implementation code is as follows:
Import lombok.Data;import java.util.ArrayList;import java.util.HashSet;import java.util.List;public class DistinctExample {public static void main (String [] args) {/ / create and assign List List list = new ArrayList (); list.add (new Person ("Li Si", "123456", 20); list.add ("Zhang San", "123456", 18)) List.add (new Person ("Wang Wu", "123456", 22)); list.add (new Person ("Zhang San", "123456", 18)); / / deduplication operation HashSet set = new HashSet (list); / / print collection information set.forEach (p-> System.out.println (p));} @ Dataclass Person {private String name; private String password Private int age; public Person (String name, String password, int age) {this.name = name; this.password = password; this.age = age;}}
The result of the above program execution is shown in the following figure:
From the above results, we found a problem that the order of elements changed after using HashSet to remove weights. To solve this problem, we can use LinkedHashSet to implement the deduplication function, as shown in the following code:
Import lombok.Data;import java.util.ArrayList;import java.util.LinkedHashSet;import java.util.List;public class DistinctExample {public static void main (String [] args) {/ / create and assign List List list = new ArrayList (); list.add (new Person ("Li Si", "123456", 20); list.add ("Zhang San", "123456", 18)) List.add (new Person ("Wang Wu", "123456", 22)); list.add (new Person ("Zhang San", "123456", 18)); / / deduplication operation LinkedHashSet set = new LinkedHashSet (list); / / print collection information set.forEach (p-> System.out.println (p));} @ Dataclass Person {private String name; private String password Private int age; public Person (String name, String password, int age) {this.name = name; this.password = password; this.age = age;}}
The result of the above program execution is shown in the following figure:
3. Use Stream to remove weight
The last and simplest method is to remove duplicates. We can use Stream provided in JDK 8 to remove duplicates. A method for removing duplicates is included in Stream: distinct, which can directly implement the deduplication function of the collection. The specific implementation code is as follows:
Import lombok.Data;import java.util.ArrayList;import java.util.List;import java.util.stream.Collectors;public class DistinctExample {public static void main (String [] args) {/ / create and assign List List list = new ArrayList (); list.add (new Person ("Li Si", "123456", 20); list.add ("Zhang San", "123456", 18)) List.add ("Wang Wu", "123456", 22); list.add (new Person ("Zhang San", "123456", 18)); / / deduplication operation list = list.stream (). Distinct (). Collect (Collectors.toList ()); / / print collection information list.forEach (p-> System.out.println (p)) } @ Dataclass Person {private String name; private String password; private int age; public Person (String name, String password, int age) {this.name = name; this.password = password; this.age = age;}}
The result of the above program execution is shown in the following figure:
This is the end of the introduction of "how to remove duplicates from List sets in java". Thank you for your 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.