This article will give two methods to delete repetitive elements from ArrayList, which uses HashSet and LinkedhashSet.
ArrayList is one of the most commonly used collection types in Java. It allows to flexibly add multiple NULL elements, repeated elements, and maintain the order of element insertion. When coding, we often encounter the requirements that must delete repeat elements from the built -up ArrayList.
Method 1: Use HashSet to delete repeated elements in ArrayList
In this method, we use HashSet to delete repeated elements. As you know, HashSet does not allow repeated elements. We use this attribute of HashSet to delete the duplicate elements in the built -in ArrayList. However, this method has a disadvantage. That is, it will delete the insertion order of elements in ArrayList. This means that after deleting repeated elements, the order of the element is not right. Let's look at the following example.
Import java.util.arrayList; Import Java.util.hashSet; Public Class MainClass {Public Static Void Main (String [] ARGS) {// Constructing An ARRAYList A RRAYLIST <string> ListWithDuplicateElements = New ArrayList <string> (); ListWithDuplicateElements. add ("java"); listwithDuplicateements.add ("J2EE"); listwithDuplicateements.add ("JSP"); thDuplicateElements.add ("Java"); listwithDuplicateEyEments.add ("Struts" ("Struts" );; // Printing listwithDuplicateements System.Print ("ArrayList with Duplicate Elements:"); Rintln (ListWithDuplicateElements); // Construction HashSet USING ListWithDuplicateEMEEMETS HASHSET <string> Set = New HashSet <string> (ListWithDuplicateements); // Construction ListWithoutDuplicatements using set arraylist <string> TS = New ArrayList <string> (set); // Printing listwithoutDuplicateements System.out.print ("ArrayList after remning duplicate elements:") ; System.out.println (listwithoutDuplicateements);}}Output:
ArrayList with duplicate elements: [Java, J2EE, JSP, Servlets, Java, Struts, JSP] ArrayList after rematching duplicate elements: [java, jsp, j2ee, str UTS]
Pay attention to the output results. You will find that after deleting duplicate elements, the element is reshuffled. No longer arranged in the order of insertion. If you want to keep the order of elements after deleting repeated elements, it is not recommended to use this method. There is another method that can ensure that the insertion order of the element does not change after deleting the repeated elements. That is to use Linkedhashset.
Method 2: Use linkedhashset to delete the repeated elements in ArrayList
In this method, we use Linkedhashset to delete repeated elements in ArrayList. As you know, Linkedhashset does not allow repetitive elements, while maintaining the order of element insertion. These two attributes of Linkedhashset can ensure that after deleting repetitive elements in ArrayList, the order of insertion elements is still maintained. See the following example.
Import java.util.arrayList; Import Java.util.linkedhaSet; Public Class MainClass {Public Static Void Main (String [] ARGS) YLIST ARRAYList <string> ListWithDuplicateements = New ArrayList <string> (); ListwithDuplicateElements. add ("java"); listwithDuplicateements.add ("J2EE"); listwithDuplicateements.add ("jsp"); thDuplicateElements.add ("Java"); listwithDuplicateEyEments.add ("Struts" ("Struts" );; // Printing listwithDuplicateements System.Print ("ArrayList with Duplicate Elements:"); Rintln (ListWithDuplicateElements); // Construction LinkedhaShSet users = New LinkedhashSet <string> (ListWithDuplicateElements); // Construction ListWithoutDuplicateements USING SET ArrayList <string> EELEMENTS = New ArrayList <string> (set); // Printing listwithoutDuplicateements System.out.print ("ArrayList after Removing Duplicate Elements:") ; System.out.println (listwithoutDuplicateements);}}Output:
ArrayList with duplicate elements: [Java, J2EE, JSP, Servlets, Java, Struts, JSP] ArrayList after rematching duplicate elements: [java, jspe, service UTS]
Pay attention to the output. You can find that after deleting the repetitive elements in ArrayList, the order of insertion is still maintained.
The above is all the contents of this article. I hope it will be helpful to everyone's learning.