Home > Java > javaTutorial > body text

2 ways to delete duplicate elements in ArrayList in Java

高洛峰
Release: 2017-01-22 16:08:25
Original
1327 people have browsed it

This article will give you two methods to remove duplicate elements from ArrayList, using HashSet and LinkedHashSet.

ArrayList is one of the most commonly used collection types in Java. It allows flexible addition of multiple null elements, duplicate elements, and maintains the insertion order of elements. When coding, we often encounter the requirement that duplicate elements must be removed from an ArrayList that has been built.

Method 1: Use HashSet to delete duplicate elements in ArrayList

In this method, we use HashSet to delete duplicate elements. As you know, HashSet does not allow duplicate elements. We use this property of HashSet to delete duplicate elements in the built ArrayList. However, there is a drawback to this approach. That is, it removes the insertion order of elements in the ArrayList. This means that after removing duplicate elements, the elements are inserted in the wrong order. Let’s take a look at the following example first.

import java.util.ArrayList;
import java.util.HashSet;
  
public class MainClass
{
 public static void main(String[] args)
 {
 //Constructing An ArrayList
  
 ArrayList<String> listWithDuplicateElements = new ArrayList<String>();
  
 listWithDuplicateElements.add("JAVA");
  
 listWithDuplicateElements.add("J2EE");
  
 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>(listWithDuplicateElements);
  
 //Constructing listWithoutDuplicateElements using set
  
 ArrayList<String> listWithoutDuplicateElements = new ArrayList<String>(set);
  
 //Printing listWithoutDuplicateElements
  
 System.out.print("ArrayList After Removing Duplicate Elements :");
  
 System.out.println(listWithoutDuplicateElements);
 }
}
Copy after login


Output:

ArrayList With Duplicate Elements :[JAVA, J2EE, JSP, SERVLETS, JAVA, STRUTS, JSP]
ArrayList After Removing Duplicate Elements :[JAVA, SERVLETS, JSP, J2EE, STRUTS]
Copy after login

Pay attention to the output. You'll notice that after removing duplicate elements, the elements are reshuffled. No longer arranged in insertion order. This method is not recommended if you want to maintain the insertion order of elements after removing duplicate elements. There is another way to ensure that the insertion order of elements is not changed after removing duplicate elements. That is to use LinkedHashSet.

Method 2: Use LinkedHashSet to delete duplicate elements in ArrayList

In this method, we use LinkedHashSet to delete duplicate elements in ArrayList. As you know, LinkedHashSet does not allow duplicate elements while maintaining the insertion order of elements. These two properties of LinkedHashSet can ensure that the insertion order of elements is still maintained after deleting duplicate elements in ArrayList. See example below.

import java.util.ArrayList;
import java.util.LinkedHashSet;
  
public class MainClass
{
 public static void main(String[] args)
 {
 //Constructing An ArrayList
  
 ArrayList<String> listWithDuplicateElements = new ArrayList<String>();
  
 listWithDuplicateElements.add("JAVA");
  
 listWithDuplicateElements.add("J2EE");
  
 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<String>(listWithDuplicateElements);
  
 //Constructing listWithoutDuplicateElements using set
  
 ArrayList<String> listWithoutDuplicateElements = new ArrayList<String>(set);
  
 //Printing listWithoutDuplicateElements
  
 System.out.print("ArrayList After Removing Duplicate Elements :");
  
 System.out.println(listWithoutDuplicateElements);
 }
}
Copy after login


Output:

ArrayList With Duplicate Elements :[JAVA, J2EE, JSP, SERVLETS, JAVA, STRUTS, JSP]
ArrayList After Removing Duplicate Elements :[JAVA, J2EE, JSP, SERVLETS, STRUTS]
Copy after login

Pay attention to the output. You can find that after deleting duplicate elements in the ArrayList, the insertion order of the elements is still maintained.

The above is the entire content of this article, I hope it will be helpful to everyone’s study.

For more 2 Java methods to delete duplicate elements in ArrayList, please pay attention to the PHP Chinese website for related articles!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!