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.
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.
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.
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
- 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)
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
Output: