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 duplicate the List collection in Java

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

Share

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

Editor to share with you how to re-List collection in Java, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

01

Implementation idea: use two for loops to iterate through all the elements of the collection, and then determine whether there are the same elements, and if so, remove them. This approach is the first thing that comes to mind and is the simplest way to implement it. Among them, this approach ensures that the original order of List collections remains the same.

Code implementation:

/ * notes: List weight removal using two for loops * @ param list* @ return*/public static List repeatListWayOne (List list) {for (int I = 0 for I < list.size (); iSum +) {for (list.get (I) .equals (list.get (j)) {list.remove (j);}} return list;}

02

Implementation idea: we know that HashSet implements the Set interface and does not allow duplicate elements. Based on this idea, you can store all the elements of the List collection in the HashSet object, then empty all the List collection elements, and finally add all the HashSet object elements to the List collection, so that there are no duplicate elements. HashSet has a constructor that can add elements directly during initialization. Among them, HashSet cannot guarantee that the order remains the same, so this approach does not guarantee that the original order of List collections remains the same.

Code implementation:

/ * notes: use HashSet to implement List deduplication * @ param list* @ return*/public static List repeatListWayTwo (List list) {/ / initialize the HashSet object and assign list object elements to the HashSet object HashSet set = new HashSet (list); / / empty list.clear () all elements of the List collection; / / add the HashSet object to the List collection list.addAll (set); return list;}

03

Implementation idea: the TreeSet collection also implements the Set interface, which is an orderly and non-repetitive collection of elements. By the same token, we can remove the weight according to the thought of mode 2 above. Among them, the deduplicated List collection can be guaranteed to be consistent with the original order.

Code implementation:

/ * notes: use TreeSet to implement List deduplication * @ param list* @ return*/public static List repeatListWayThird (List list) {/ / initialize the TreeSet object and assign list object elements to the TreeSet object TreeSet set = new TreeSet (list); / / empty list.clear () all elements of the List collection; / / add the TreeSet object to the List collection list.addAll (set); return list;}

04

Implementation idea: use the List set contains method to cycle through, first create a new List collection, and then cycle through the original List collection to determine whether the new set contains the old set, if so, do not add to the new collection, otherwise add. Finally, empty the old collection and assign the new collection elements to the old collection.

Code implementation:

/ * notes: use the List collection contains method to cycle through * @ param list* @ return*/public static List repeatListWayFourth (List list) {/ / create a new List collection to store the deduplicated element List newList = new ArrayList (); / / cycle through the old collection element for (int I = 0; I < list.size ()) If not, save boolean isContains = newList.contains (list.get (I)); if (! isContains) {newList.add (list.get (I));}} / / empty list.clear () all elements of the List collection; / / add new collection elements to the List collection list.addAll (newList); return list;}

The above introduces you four ways to remove duplicates from List collections. So, which way is the most efficient? Here's a demonstration to make a comparison.

For demonstration purposes, 20000 integer strings between 0,500 and 500 are randomly generated, stored in the List collection, and compared in the relevant printing time of the corresponding code. The randomly generated List collection code is as follows:

/ * randomly generate 20000 integer strings between 0,500 and store them in the List collection * @ return*/public static List getRandomList () {List list = new ArrayList (); / / randomly generate 20000 integer strings for (int I = 1; I)

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