In this tutorial, we will learn to write a Java program to remove duplicates from an ArrayList, we will learn to remove duplicate elements by various approaches.
ArrayList is a collection type used mostly in java. It is the list interface from Java’s collection framework that allows duplicates in its list. It provides insertion order, flexibility in the program, and duplicates in a list.
We will see two ways to remove duplicates in java ArrayList
- Using HashSet
- Using LinkedHashSet
1. Remove duplicate elements using HashSet in ArrayList in java
HashSet is one of the methods to remove elements from ArrayList. It does not follow insertion order which is the disadvantage of using it because once the duplicates elements are removed, the elements will not be in an insertion order inside ArrayList.
Program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | import java.util.ArrayList; import java.util.HashSet; public class HashSetDuplicates { public static void main(String[] args) { //ArrayList ArrayList<String> ListwithDuplicates = new ArrayList<String>(); /* Duplicate elements in the following ArrayList Rick, Dayrl, Glenn */ ListwithDuplicates.add("Rick"); ListwithDuplicates.add("Glenn"); ListwithDuplicates.add("Dayrl"); ListwithDuplicates.add("Carol"); ListwithDuplicates.add("Rick"); ListwithDuplicates.add("Dayrl"); ListwithDuplicates.add("Maggie"); ListwithDuplicates.add("Glenn"); //Constructing HashSet HashSet<String> set = new HashSet<String>(ListwithDuplicates); //List with no duplicate elementsusing set ArrayList<String> listWithNoDuplicates = new ArrayList<String>(set); //Display list With No Duplicate Elements in ArrayList System.out.println("After removal of Duplicates "); System.out.println(listWithNoDuplicates); } } |
The output of removing duplicate elements using HashSet:
After removal of Duplicates
[Rick, Dayrl, Glenn, Carol, Maggie]
2. Remove duplicate elements using LinkedHashSet in ArrayList in java
LinkedHashSet is also one of the other methods to remove elements from ArrayList. The advantage of using LinkedHashSet is that it does not allow duplicates and also maintains insertion order. It is the useful property of LinkedHashSet that is used in removing Duplicates elements from ArrayList.
Program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | import java.util.ArrayList; import java.util.LinkedHashSet; public class LinkedHashSetDuplicates { public static void main(String[] args) { //ArrayList ArrayList<String> ListwithDuplicates = new ArrayList<String>(); /* Duplicate elements in the following ArrayList Rick, Dayrl, Glenn */ ListwithDuplicates.add("Rick"); ListwithDuplicates.add("Glenn"); ListwithDuplicates.add("Dayrl"); ListwithDuplicates.add("Carol"); ListwithDuplicates.add("Rick"); ListwithDuplicates.add("Dayrl"); ListwithDuplicates.add("Maggie"); ListwithDuplicates.add("Glenn"); //Constructing HashSet LinkedHashSet<String> set = new LinkedHashSet<String>(ListwithDuplicates); //List with no duplicate elementsusing set ArrayList<String> listWithNoDuplicates = new ArrayList<String>(set); //Display list With No Duplicate Elements in ArrayList System.out.println("After removal of Duplicates "); System.out.println(listWithNoDuplicates); } } |
Output:
After removal of Duplicates
[Rick, Glenn, Dayrl, Carol, Maggie]