Reprint Please specify source: http://blog.csdn.net/l1028386804/article/details/47414935
ArrayList is one of the most commonly used collection types in Java. It agrees to flexibly add multiple null elements, repeating elements, and keeping elements in the order of insertion. We often encounter the need to remove repeated elements from a built-in ArrayList when coding. This article will give two ways to remove repeated elements from ArrayList.
Method 1: Use HashSet to delete the repeated elements in the ArrayList in the method. We use HashSet to remove repeated elements.
As you know, HashSet does not agree with repeated elements. We use this property of HashSet to delete the repeated elements in the ArrayList that have been built. However, there is a drawback to such a method. That is, it removes the insertion order of the elements in the ArrayList.
This means that. When you delete repeated 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 repeated elements, the elements shuffle again. No longer arranged in the order of insertion. It is not recommended to use this method if you want to retain the insertion order of elements after you delete the repeated elements. There is also a way to ensure that the element's insertion order is not changed after the repeated elements have been deleted. That's using Linkedhashset.
Method 2: Use Linkedhashset to delete repeated elements in ArrayList
In this method. We use Linkedhashset to remove repeated elements in ArrayList. As you know, Linkedhashset repeats elements, preserving the insertion order of the elements at the same time. These two properties of linkedhashset ensure that the repeated elements in the ArrayList are removed. Still retains the insertion order of the elements.
See below for examples.
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 now delete the repeated elements in the ArrayList. Still retains the insertion order of the elements.
Java--2 ways to delete repeated elements in ArrayList