How to remove duplicate elements from an ArrayList in Java.
Ø
ArrayList allows adding duplicate elements,
sometime there may be a requirement to remove duplicates from a list.
Ø In this
post will talk about the way to do it.
1St Approach:
Ø First thing that will come to mind is create a new list and loop through the created old list.
Ø Use contains() method to check if element is already existed to the new list.
Ø If element is not existed then add it to the new list otherwise discard it.
Program:
package com.coding.core;
import java.util.ArrayList;
import java.util.List;
public class RemoveDuplicateElements {
public static void main(String[] args) {
List<String> gameList = new ArrayList<String>();
gameList.add("Portal");
gameList.add("Chess");
gameList.add("Roulette");
gameList.add("poker");
gameList.add("poker");
gameList.add("poker");
gameList.add("Carrom");
// Create a new ArrayList
System.out.println("********************************************************");
List<String> newGameList = new ArrayList<String>();
for (String gameName : gameList) {
if (!newGameList.contains(gameName)) {
newGameList.add(gameName);
}
System.out.println("Duplicate gameList : " + gameName);
}
System.out.println("********************************************************");
for (String uniqueGameName : newGameList) {
System.out.println("Game Names : " + uniqueGameName);
}
System.out.println("********************************************************");
}
OUTPUT :
********************************************************************************
Game Name : Portal
Game Name : Chess
Game Name : Roulette
Game Name : poker
Game Name : Carrom
********************************************************************************
2nd Approach :
But that much code is really not required.
Collection framework itself provides several
options to remove duplicates from an arraylist.
We can simply use a HashSet to do the trick of
removing the duplicate elements.
HashSet only allows unique elements and we'll
use that feature of HashSet to remove duplicates.
Only problem is it won't retain the order of the
list.
Program:
package com.coding.core;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class RemovingDuplicatesByUsingHashSet {
public static void main(String[] args) {
List<String> gameList = new ArrayList<String>();
gameList.add("Portal");
gameList.add("Chess");
gameList.add("Roulette");
gameList.add("poker");
gameList.add("poker");
gameList.add("poker");
gameList.add("Carrom");
// Create HashSet
Set<String> gameSet = new HashSet<String>(gameList);
// remove all the elements from the list
gameList.clear();
gameList.addAll(gameSet);
System.out.println("********************************************************");
for (String name : gameList) {
System.out.println("Game Name - " + name);
}
System.out.println("********************************************************");
}
========================================================================
If you want retain the order then you will go with LinkedHashSet:
Please find below program:
package com.coding.core;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
public class RemovingDuplicatesByUsingHashSet {
public static void main(String[] args) {
// TODO Auto-generated method stub
List<String> gameList = new ArrayList<String>();
gameList.add("Portal");
gameList.add("Chess");
gameList.add("Roulette");
gameList.add("poker");
gameList.add("poker");
gameList.add("poker");
gameList.add("Carrom");
// Create HashSet
//Set<String> gameSet = new HashSet<String>(gameList);
Set<String> gameSet = new LinkedHashSet<String>(gameList);
// remove all the elements from the list
gameList.clear();
gameList.addAll(gameSet);
System.out.println("********************************************************");
for (String name : gameList) {
System.out.println("Game Name - " + name);
}
System.out.println("********************************************************");
} }
Out Put :
********************************************************
Game Name - Portal
Game Name - Chess
Game Name - Roulette
Game Name - poker
Game Name - Carrom
********************************************************
Java 8 streams provide a very simple way to remove duplicate
elements from a list.
Using the distinct method.
package com.coding.core.sort;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class Java8RemoveDuplicates {
public static void main(String[] args) {
// TODO Auto-generated method stub
List<String> gameList = new ArrayList<String>();
gameList.add("Portal");
gameList.add("Chess");
gameList.add("Roulette");
gameList.add("poker");
gameList.add("poker");
gameList.add("poker");
gameList.add("Carrom");
gameList = gameList.stream().distinct().collect(Collectors.toList());
System.out.println("*****************************************************");
for (String name : gameList) {
System.out.println("Game Name - " + name);
}
System.out.println("******************************************************");
}
}}
output:
******
********************************************************
Game Name - Portal
Game Name - Chess
Game Name - Roulette
Game Name - poker
Game Name - Carrom
********************************************************
you can see that the duplicate element is removed and
original order is retained too and all that is done in a single line.