How to Remove Duplicates from ArrayList in Java

ArrayList is most used class from Java's Collection framework, but it allows duplicates.There are many ways to remove duplicates from the ArrayList.

1). Using Set

Set which is primarily designed to store unique elements,To remove duplicates objects from ArrayList ,copy them to a Set like HashSet and then copy it back to ArrayList

public class RemoveDuplicates1 { public static void main(String[] args) { List <String> fruitsList = new ArrayList <String>(); fruitsList.add("Mango"); fruitsList.add("Kiwi"); fruitsList.add("Mango"); fruitsList.add("Banana"); fruitsList.add("Watermellon"); fruitsList.add("Apple"); System.out.println(fruitsList.toString()); Set <String> fruitsSet = new LinkedHashSet <String> (fruitsSet); fruitsList.clear(); fruitsList.addAll(fruitsSet); System.out.println("The fruits list without duplicates : "+fruitsList); } }

2). Using Iterator

In iterator we have traverse each element in the arraylist and put the element to new arraylist.Before put element to new arrayList check if element present in new list.For this arralist has contains() method.

1). Create new ArrayList.

2). Traverse through the first arraylist and store the first appearance of each element into the second arraylist using contains() method.

3). The second ArrayList contains the elements with duplicates removed.

import java.util.* ; public class RemoveDuplicates2 { // dFunction to remove duplicates from an ArrayList public static ArrayList <String> removeDuplicates(ArrayList <String> fruitsList) { ArrayList <String> newFruitsList = new ArrayList <String> (); for (String element: list) { // If this element is not present in newList // then add it if (!newFruitsList.contains(element)) { newFruitsList.add(element); } } return newFruitsList; } public static void main(String args[]) { List <String> fruitsList = new ArrayList <String> (); fruitsList.add("Mango"); fruitsList.add("Kiwi"); fruitsList.add("Mango"); fruitsList.add("Apple"); fruitsList.add("Banana"); fruitsList.add("Watermellon"); fruitsList.add("Apple"); fruitsList.add("Kiwi"); System.out.println("Original ArrayList with duplicates: " + fruitsList); // Remove duplicates ArrayList <String> newFruitsList = removeDuplicates(fruitsList); // Print the ArrayList without duplicates System.out.println("New ArrayList without duplicates : " + fruitsList); } }

3). Using Java 8 Stream.distinct()

Java 8 Stream contains distinct method which removes duplocated from the arrayList. The distinct() method return a new Stream without duplicates elements based on the result returned by equals() method.

import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; class RemoveDuplicates3 { public static void main(String[] args) { List <String> fruitsList = new ArrayList<String>(); fruitsList.add("Mango"); fruitsList.add("Kiwi"); fruitsList.add("Mango"); fruitsList.add("Apple"); fruitsList.add("Banana"); fruitsList.add("Watermellon"); fruitsList.add("Apple"); fruitsList.add("Kiwi"); System.out.println("Original ArrayList with duplicates : " + fruitsList); List <String> newfruitsList = fruitsList.stream().distinct().collect(Collectors.toList()); System.out.println("ArrayList without duplicates : " + newfruitsList); } }

Post/Questions related to How to Remove Duplicates from ArrayList in Java

How do you remove duplicates in ArrayList without collections?
How do you remove duplicates from ArrayList?
Remove duplicates from ArrayList of objects java
Remove duplicates from ArrayList in java without using the set
How to remove duplicate objects from ArrayList in java 8
How to remove duplicate objects from ArrayList in java using the comparator
How to remove duplicates from array in java
Differences between ArrayList and Vector
Remove elements from ArrayList and CopyOnWriteArrayList
Java ArrayList removeAll() Examples
Java ArrayList Tutorial with Examples
Remove duplicate custom objects from ArrayList in java

In this article, we have seen  How to Remove Duplicates from ArrayList in Java.