How to remove duplicate objects from ArrayList in Java

1. Remove duplicate elements in arraylist using LinkedHashSet

The LinkedHashSet is the best approach for removing duplicate elements in an arraylist. LinkedHashSet does two things internally :

  • Remove duplicate elements
  • Maintain the order of elements added to it

Java example to remove duplicates in arraylist using LinkedHashSet. In given example, numbersList is an arraylist containing integers and some of them are duplicate numbers e.g. 1, 3 and 5. We add the list to LinkedHashSet, and then get back the content back into the list. The result arraylist does not have duplicate integers.

import java.util.ArrayList; import java.util.Arrays; import java.util.LinkedHashSet; public class ArrayListExample { public static void main(String[] args) { // ArrayList with duplicate elements ArrayList<Integer> numbersList = new ArrayList<>(Arrays.asList(1, 1, 2, 3, 3, 3, 4, 5, 6, 6, 6, 7, 8)); System.out.println(numbersList); LinkedHashSet<Integer> hashSet = new LinkedHashSet<>(numbersList); ArrayList<Integer> listWithoutDuplicates = new ArrayList<>(hashSet); System.out.println(listWithoutDuplicates); } }

Program Output.

[1, 1, 2, 3, 3, 3, 4, 5, 6, 6, 6, 7, 8] [1, 2, 3, 4, 5, 6, 7, 8]

1. Introduction

In this quick tutorial, we're going to learn how to clean up the duplicate elements from a List. First, we'll use plain Java, then Guava, and finally, a Java 8 Lambda-based solution.

This tutorial is part of the “Java – Back to Basic” series here on Baeldung.

Java Program to Remove duplicate elements from ArrayList

In this example, we will learn to convert the duplicate element from the ArrayList in Java.

To understand this example, you should have the knowledge of the following Java programming topics:

  • Java ArrayList Class
  • Java Set Interface

Java Program to Remove duplicates from ArrayList

Here is our sample program to learn how to remove duplicates from ArrayList. The steps followed in the below example are:
  • Copying all the elements of ArrayList to LinkedHashSet. Why we choose LinkedHashSet? Because it removes duplicates and maintains the insertion order.
  • Emptying the ArrayList, you can use clear() method to remove all elements of ArrayList and start fresh.
  • Copying all the elements of LinkedHashSet (non-duplicate elements) to the ArrayList.
You can further join Java Programming Masterclass for a Software Engineerscourse to learn more about the ArrayList class and different algorithms to remove duplicate objects.

Please find below the complete code :

import java.util.ArrayList; import java.util.LinkedHashSet; import java.util.List; import java.util.Set; /** * Java Program to remove repeated elements from ArrayList in Java. * * @author WINDOWS 8 */ public class ArrayListDuplicateDemo{ public static void main(String args[]){ // creating ArrayList with duplicate elements List<Integer> primes = new ArrayList<Integer>(); primes.add(2); primes.add(3); primes.add(5); primes.add(7); //duplicate primes.add(7); primes.add(11); // let's print arraylist with duplicate System.out.println("list of prime numbers : " + primes); // Now let's remove duplicate element without affecting order // LinkedHashSet will guaranteed the order and since it's set // it will not allow us to insert duplicates. // repeated elements will automatically filtered. Set<Integer> primesWithoutDuplicates = new LinkedHashSet<Integer>(primes); // now let's clear the ArrayList so that we can // copy all elements from LinkedHashSet primes.clear(); // copying elements but without any duplicates primes.addAll(primesWithoutDuplicates); System.out.println("list of primes without duplicates : " + primes); } } Output list of prime numbers : [2, 3, 5, 7, 7, 11] list of primes without duplicates : [2, 3, 5, 7, 11]


In this example, you can see we have created an ArrayList and added numbers into it, all prime numbers. We added '7' twice, so that it become duplicate. Now we print the ArrayList and you can see that it contains number 7 twice.

How to remove duplicate objects from ArrayList in Java


After that we created a LinkedHashSet from our ArrayList, clear our original ArrayList and then added all elements from set to the list. This time we should not have any duplicates because Set doesn't allow them and they should have filtered when elements copied from ArrayList to HashSet by Java. This is proved by printing the ArrayList again, now it doesn't contain 7 twice, but only once.

That's all about how to remove duplicates from ArrayList in Java. Though there are multiple ways to do this, I think using LinkedHashSet is the simplest one because its simple and also preserve the order of elements.

Further Learning
Java In-Depth: Become a Complete Java Engineer
Java Fundamentals: Collections
Data Structures and Algorithms: Deep Dive Using Java


If you are interested in learning ArrayList, you should check out my following tutorials :
  • What is difference between two remove() methods of ArrayList class? (answer)
  • What is the correct way to remove objects from ArrayList while Iterating? (answer)
  • How to get rid of repeated elements from ArrayList? (solution)
  • How to reverse an ArrayList in Java? (solution)
  • How to synchronize ArrayList in Java? (answer)
  • Difference between Array and ArrayList in Java? (answer)
  • What is the difference between ArrayList and HashSet in Java? (answer)
  • What is the difference between HashMap and ArrayList? (answer)
  • How to convert String ArrayList to String Array in Java? (answer)
  • Beginners Guide to ArrayList in Java (guide)
  • How to get a sublist from ArrayList in Java? (program)
  • When to use ArrayList over LinkedList in Java? (answer)
  • How to create and initialize ArrayList in one line? (trick)
  • How to sort ArrayList of Integers in ascending order? (solution)
  • What is difference between Vector and ArrayList in Java? (answer)
  • How to loop ArrayList in Java? (solution)
  • How to convert an ArrayList to String in Java? (solution)
  • Array's length() vs ArrayList size() method (read here)
  • What is CopyOnWriteArrayList in Java? When do you use it? (answer)
  • How and when to use ArrayList in Java? (answer)
  • How to make read-only ArrayList in Java? (trick)
  • 3 ways to traverse List in Java? (examples)
  • How to convert List to Set in Java? (example)
Thanks for reading this ArrayList tutorial. You have seen how easy it is to remove duplicate elements from ArrayList in Java, you just need to change the data structure. In fact, if you need unique elements to prefer a Set vs List but sometimes you also need benefits of ArrayList like fast search in constant time and that's where this technique can help you.

1. Stream distinct() method

  • Stream’s distinct() method returns a stream consisting of the distinct elements (according toObject.equals(Object)) of this stream
  • Below example removes duplicate String elements and maintains original insertion order
  • Terminal operation :- Another Stream method collect() is used to collect String elements into new list
  • Similarly, sorted() method of Stream helps to sort String elements in alphabetical order once after removing duplicates
package net.bench.resources.java8; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; public class RemoveDuplicatesFromArrayList { public static void main(String[] args) { // create test data List<String> students = new ArrayList<String>(); // add values to list students.add("Roger"); students.add("Rafael"); students.add("Djokovic"); students.add("Roger"); students.add("Murray"); students.add("Rafael"); // pretty print System.out.println("1. Original list with duplicate values :\n"); students.forEach(student -> System.out.println(student)); // Java 8 - distinct() method List<String> uniqueList = students .stream() // get stream for original list .distinct() // distinct method removes duplicates .collect(Collectors.toList()); // distinct elements stored to new list // pretty print System.out.println("\n2. New list with unique values" + " maintaining original insertion order:\n"); uniqueList.forEach(uniqueStudent -> System.out.println(uniqueStudent)); // Java 8 - sorting List<String> sortedList = students .stream() // get stream for original list .distinct() // distinct method removes duplicates .sorted() // uses natural-ordering to sort .collect(Collectors.toList()); // distinct elements stored to new list // pretty print System.out.println("\n3. New list with unique values" + " in natural sorting order :\n"); sortedList.forEach(sortedStudent -> System.out.println(sortedStudent)); } }

Output:

1. Original list with duplicate values : Roger Rafael Djokovic Roger Murray Rafael 2. New list with unique values maintaining original insertion order: Roger Rafael Djokovic Murray 3. New list with unique values in natural sorting order : Djokovic Murray Rafael Roger