Various examples of deduplicating an ArrayList in Java.
Tutorial Contents
Overview
Java List is an ordered collection of elements that may contain duplicate elements. Java List is widely used to hold a collection of values or objects. This tutorial describes ways to find and remove duplicate elements from a Java List implementation.
Using Stream to Remove Duplicates in a List
Java Stream interface provides many useful Intermediate Operations that we can use to process, filter, or manipulate Stream elements. The ‘distinct()‘ method of the Stream returns a new Stream of unique elements. Thus, we can use it to remove duplicates from our Java List.
Example of using Java Stream distinct() to remove duplicate elements from a List
List<Integer> list = List.of(1, 2, 4, 2, 1, 4, 2, 5, 6, 4)
List<Integer> output = list.stream()
.distinct()
.toList();
output.forEach(System.out::print);
//prints:
//12456
Code language: Java (java)
We created a Stream of all elements from the List and invoked the ‘distinct()‘ function before collecting the deduplicated Java List.
Please, note that we created a new List containing all the unique elements from the first List, keeping it unchanged.
Using a Java Set to Remove Duplicates in a List
Alternatively, we can use one of the Java Set implementations to deduplicate a Java List. Java Set is a distinct collection of elements.
Please note that we can use HashSet to remove duplicate elements from a List, but it is an unordered collection and will not honour the order of the elements in the List.
A LinkedHashSet, on the other hand, is an ordered collection of unique elements. We can safely use it to remove duplicate items from a List.
Example of using a LinkedHashSet to remove Java List’s duplicate elements.
List<Integer> list = List.of(2, 3, 4, 2, 4, 5, 6, 3);
List<Integer> output = new ArrayList<>(
new LinkedHashSet<>(list));
output.forEach(System.out::print);
//prints:
//12456
Code language: Java (java)
When we create a LinkedHashSet instance using the List, it removes all the duplicates from the List and maintains the order of the elements. We then used the LinkedHashSet instance in the ArrayList constructor to build a new List of unique elements.
Summary
This quick tutorial described two ways of removing duplicate elements from a Java ArrayList instance. In the first example, we used Java Streams distinct() method, and we used a LinkedHashSet instance in the next.
You can refer to our GitHub Repository for the complete source code of the examples used in this tutorial.