Two methods of removing duplicate data within ArrayList

zhaozj2021-02-16  91

Expertise: intermediate

Language: Java

Two Methods to Remove Duplicates in an ArrayList

Here Are Two Methods That Allow You to Remove Duplicates in an ArrayList.

RemoveDuPlicate Does Not maintain the Order Where AS

RemoveduplicateWithOrder Maintains The Order with Some Performance Overhead.

1.The Removeduplicate Method: / ** list ORDER NOT MAINTAINED ** / Public Static Void RemoveDuplicate (arraylist arllist) {hashset h = new hashset (arllist); arllist.clear (); arllist.addall (h);}

2.The RemoveduplicateWithOrder method:

/ ** list ORDER MainTained ** /

public static void removeDuplicateWithOrder (ArrayList arlList) {Set set = new HashSet (); List newList = new ArrayList (); for (Iterator iter = arlList.iterator (); iter.hasNext ();) {Object element = iter.next (); Set.add (element)) newlist.add (element); arllist.clear (); arllist.addall (newlist);}

Vijayanandraj Amaladoss

转载请注明原文地址:https://www.9cbs.com/read-13215.html

New Post(0)