Java

How to Remove Duplicate Elements from List in Java

This tutorial covers a different techniques to Remove Duplicate Elements from a List in Java.

Overview

Java Lists are not distinct collections and they can contain duplicates. In this quick tutorial we will learn to remove duplicate elements from a Java List. First, we will use Java Streams to deduplicate java list. Next, we will use a Set to remove duplicates.

Remove Duplicates using Java Stream

We can use the distinct method of Streams to remove the duplicate elements from an ArrayList.

List<Integer> list = List.of(2, 3, 4, 2, 4, 5, 6, 3); List<Integer> output = list.stream() .distinct() .collect(Collectors.toList());
Code language: Java (java)

First, we created an ArrayList with duplicate elements. Next, we created a stream of elements and called distinct method on the stream. Finally, we collect the elements into a new ArrayList.

The the collected list is deduplicated and it looks like this.

[2, 3, 4, 5, 6]

Note that we are creating a new list here. Thus the original list with duplicate remains unchanged.

Remove Duplicates using Java Set

We all know that Java Set is a distinct collection of elements. However, the most popular Set implementation, HashSet is unordered. Which means, it cannot guarantee the order of insertion is preserved. Hence we will use LinkedHashSet which is a unique collection of elements as well as it maintains the order of insertion.

List<Integer> list = List.of(2, 3, 4, 2, 4, 5, 6, 3); List<Integer> output = new ArrayList<>( new LinkedHashSet<>(list));
Code language: Java (java)

The output list will look like this

[2, 3, 4, 5, 6]

In this example, we first created an implementation of a LinkedHashSet by providing our list with argument. Next, we created a new ArrayList by passing the set object to get the deduplicated elements in the form of list.
Similar to the previous example, the output is a totally new list. The original list with duplicates remains unchanged.

Summary

In this quick tutorial we have covered two quick ways of deduplicating Java ArrayList or in other word removing duplicates from a Java List. First, we used Java Streams and then Java LinkedHashSet to remove duplicates.