Reprint Please specify source: http://blog.csdn.net/l1028386804/article/details/47414935
ArrayList is one of the most common collection types in Java. It allows flexibility to add multiple null elements, duplicate elements, and keep elements in the order of insertion. When coding, we often encounter the requirement that duplicate elements must be removed from the built-in ArrayList. This article will give two ways to remove duplicate elements from ArrayList.
Method 1: Use HashSet to remove duplicate elements from ArrayList in this method, we use HashSet to remove the duplicate elements. As you know, HashSet is not allowed to have duplicate elements. We use this property of HashSet to remove duplicate elements from the ArrayList that have been built. However, this approach has one drawback. That is, it removes the insertion order of the elements in the ArrayList. This means that after removing the duplicate elements, the insertion order of the elements is incorrect. Let's take a look at the following example
Import Java.util.arraylist;import Java.util.hashset;public class mainclass{public static void Main (string[] args) { Constructing an arraylistarraylist<string> listwithduplicateelements = new arraylist<string> (); ListW Ithduplicateelements.add ("JAVA"); Listwithduplicateelements.add ("ee"); Listwithduplicateelements.add ("JSP"); Listwithduplicateelements.add ("Servlets"); Listwithduplicateelements.add ("JAVA"); Listwithduplicateelements.add ( "STRUTS"); Listwithduplicateelements.add ("JSP");//printing listWithDuplicateElementsSystem.out.print ("ArrayList With Duplicate Elements: "); System.out.println (listwithduplicateelements);//constructing HashSet using listwithduplicateelementshashset< string> set = new hashset<string> (listwithduplicateelements);//constructing listwithoutduplicateelements Using setarraylist<string> listwithoutduplicateelements = new arraylist<string> (set);//Printing ListWithoutDuplicateElementsSystem.out.print ("ArrayList after RemoVing Tsun Duplicate Elements: "); System.out.println (listwithoutduplicateelements); }}
Output:
ArrayList with Duplicate Elements: [Java, EE, JSP, Servlets, Java, STRUTS, JSP]
ArrayList after removing Duplicate Elements: [JAVA, Servlets, JSP, EE, STRUTS]
Note the output results. You will find that after removing the duplicate elements, the elements are re-shuffled. No longer arranged in the order of insertion. This method is not recommended if you want to retain the insertion order of elements after deleting the duplicate elements. There is another way to ensure that duplicate elements are removed without changing the order in which they are inserted. That's using Linkedhashset.
Method 2: Use Linkedhashset to delete duplicate elements in ArrayList
In this method, we use Linkedhashset to delete the duplicate elements in the ArrayList. As you know, Linkedhashset does not allow repeating elements, while preserving the order in which elements are inserted. These two properties of the Linkedhashset ensure that the insertion order of the elements remains after the duplicate elements in the ArrayList have been deleted. See the example below.
Import Java.util.arraylist;import Java.util.linkedhashset;public class mainclass{public static void Main (string[] args ) {//constructing an arraylistarraylist<string> listwithduplicateelements = new Arraylist<string> () ; Listwithduplicateelements.add ("JAVA"); Listwithduplicateelements.add ("ee"); Listwithduplicateelements.add (" JSP "); Listwithduplicateelements.add (" Servlets "); Listwithduplicateelements.add (" JAVA "); Listwithduplicateelements.add ("STRUTS"); Listwithduplicateelements.add ("JSP");//printing ListWithDuplicateElementsSystem.out.print ("ArrayList with Duplicate Elements:"); System.out.println (listwithduplicateelements);//constructing Linkedhashset using listwithduplicateelementslinkedhashset<string> set = new Linkedhashset<string> ( listwithduplicateelements);//constructing listwithoutduplicateelements using setarraylist<string> listwithoutduplicateelements = new arraylist<string> (set);//printing ListWithoutDuplicateElementsSystem.out.priNT ("ArrayList after removing Duplicate Elements:"); System.out.println (listwithoutduplicateelements); }}
Output:
ArrayList with Duplicate Elements: [Java, EE, JSP, Servlets, Java, STRUTS, JSP]
ArrayList after removing Duplicate Elements: [JAVA, EE, JSP, Servlets, STRUTS]
Note the output. You can see that after you delete the repeating elements in the ArrayList, the insertion order of the elements remains.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java--2 ways to delete duplicate elements in ArrayList