Remove duplicates from ArrayList in Java example shows how to remove duplicate elements from ArrayList in Java using LinkedHashSet and Java 8 stream.
How to remove duplicates from ArrayList in Java?
In some situations, you do not want duplicate elements in the ArrayList object. You can remove duplicate elements from ArrayList using below given ways.
1) Using LinkedHashSet class
The Set does not allow duplicate elements, you can use this property to remove duplicate elements from the ArrayList as given below.
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 |
package com.javacodeexamples.collectionexamples; import java.util.ArrayList; import java.util.LinkedHashSet; import java.util.LinkedList; import java.util.List; public class RemoveDuplicatesFromArrayListExample { public static void main(String[] args) { List<String> aListDays = new ArrayList<String>(); aListDays.add("Sunday"); aListDays.add("Monday"); aListDays.add("Tuesday"); aListDays.add("Sunday");//duplicate aListDays.add("Wednesday"); //Convert ArrayList to LinkedHashSet LinkedHashSet<String> lhSetDays = new LinkedHashSet<String>(aListDays); //clear the original ArrayList aListDays.clear(); //Add all elements of LinkedHashSet back to the ArrayList aListDays.addAll(lhSetDays); System.out.println(aListDays); } } |
Output
1 |
[Sunday, Monday, Tuesday, Wednesday] |
We first created LinkedHashSet containing all the elements of an ArrayList using the LinkedHashSet constructor. Since the Set does not allow duplicates, it automatically added only elements that are unique. After that, we deleted all the existing elements from the ArrayList using the clear
method. Once the ArrayList is empty, we added back all the unique elements stored in the LinkedHashSet back to the ArrayList using the addAll
method.
Note: You can also use the HashSet class instead of the LinkedHashSet class. But since the ArrayList is an ordered collection, we used LinkedHashSet to retain the order of the elements. If you use the HashSet class, there will be no guarantee of the order of the elements of the ArrayList after removing duplicates. If the order of the elements is important to you, use the LinkedHashSet.
2) Using Java 8 stream and Collectors
If you are using Java 8, removing duplicates from ArrayList is just one line of code as given below.
1 2 3 4 5 6 7 8 9 10 11 |
List<String> aListDays = new ArrayList<String>(); aListDays.add("Sunday"); aListDays.add("Monday"); aListDays.add("Tuesday"); aListDays.add("Sunday");//duplicate aListDays.add("Wednesday"); aListDays = aListDays.stream().distinct().collect(Collectors.toList()); System.out.println(aListDays); |
Output
1 |
[Sunday, Monday, Tuesday, Wednesday] |
Important Note: If the ArrayList contains objects of the custom class, you need to implement the equals
and hashcode
methods in your class in order for the above approaches to work.
This example is a part of the ArrayList in Java tutorial.
Please let me know your views in the comments section below.