2 Java methods for removing duplicate elements from ArrayList _java

Source: Internet
Author: User

This article will give you two ways to remove duplicate elements from ArrayList, using HashSet and Linkedhashset respectively.

ArrayList is one of the most commonly used collection types in Java. It allows flexibility to add multiple null elements, duplicate elements, and maintain the insertion order of elements. When coding, we often encounter the requirement to remove duplicate elements from the built ArrayList.

Method 1: Use HashSet to delete duplicate elements in 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 delete the duplicate elements in the built ArrayList. However, there is a drawback to this approach. That is, it deletes the insertion order of the elements in the ArrayList. This means that after you delete the duplicate elements, the insertion order of the elements is incorrect. 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 a ArrayList arraylist<string&gt ;
 
 listwithduplicateelements = new arraylist<string> ();
 
 Listwithduplicateelements.add ("JAVA");
 
 Listwithduplicateelements.add ("Java ee");
 
 Listwithduplicateelements.add ("JSP");
 
 Listwithduplicateelements.add ("Servlets");
 
 Listwithduplicateelements.add ("JAVA");
 
 Listwithduplicateelements.add ("STRUTS");
 
 Listwithduplicateelements.add ("JSP");
 
 Printing listwithduplicateelements System.out.print ("ArrayList with Duplicate Elements:");
 
 System.out.println (listwithduplicateelements); Constructing hashset using listwithduplicateelements hashset<string> set = new Hashset<string> (ListWithD
 
 uplicateelements); Constructing listwithoutduplicateelements using Set arraylist<string> listwithoutduplicateelements = new Array
 
 List<string> (set); Printing ListwiThoutduplicateelements System.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 E Lements: [JAVA, Servlets, JSP, EE, STRUTS]


Note the output results. You'll notice that after you delete the duplicate elements, the elements shuffle again. No longer arranged in the order of insertion. This method is not recommended if you want to retain the insertion order of elements after you delete the duplicate elements. There is another way to ensure that after you delete a duplicate element, you do not change the insertion order of the elements. That's using Linkedhashset.

Method 2: Use Linkedhashset to delete duplicate elements in ArrayList

In this method, we use Linkedhashset to delete the duplicated elements in the ArrayList. As you know, Linkedhashset does not allow repeating elements, while preserving the insertion order of elements. These two properties of linkedhashset ensure that the insertion order of the elements is persisted after removing the repeating elements in the ArrayList. See the example below.

Import java.util.ArrayList;
 
Import Java.util.LinkedHashSet; public class MainClass {public static void main (string[] args) {//constructing a ArrayList arraylist<string&gt ;
 
 listwithduplicateelements = new arraylist<string> ();
 
 Listwithduplicateelements.add ("JAVA");
 
 Listwithduplicateelements.add ("Java ee");
 
 Listwithduplicateelements.add ("JSP");
 
 Listwithduplicateelements.add ("Servlets");
 
 Listwithduplicateelements.add ("JAVA");
 
 Listwithduplicateelements.add ("STRUTS");
 
 Listwithduplicateelements.add ("JSP");
 
 Printing listwithduplicateelements System.out.print ("ArrayList with Duplicate Elements:");
 
 System.out.println (listwithduplicateelements); Constructing linkedhashset using listwithduplicateelements linkedhashset<string> set = new LinkedHashSet<St
 
 Ring> (listwithduplicateelements); Constructing listwithoutduplicateelements using Set arraylist<string> listwithoutduplicateelements = new Array List<string> (SET);
 
 Printing listwithoutduplicateelements System.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 E Lements: [JAVA, EE, JSP, Servlets, STRUTS]

Note the output. You can see that after you delete the repeating elements in ArrayList, you still keep the insertion order of the elements.

The above is the entire content of this article, I hope to help you learn.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.