How to remove duplicate elements from an ArrayList

In this shot, we will learn two common ways to remove duplicate elements from an ArrayList. The methods involve the use of:

  1. Set collection
  2. The distinct method of the Stream API

1. Using a set to remove duplicate elements

We can use a setAn unordered collection of objects in which duplicates cannot be inserted. to remove the duplicate elements from an ArrayList.

In the code below, we will use LinkedHashSetOne of the implementations of set collection. to remove duplicates.

import java.util.ArrayList;
import java.util.LinkedHashSet;
class RemoveDuplicate {
public static void main( String args[] ) {
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(1);
numbers.add(2);
numbers.add(1);
numbers.add(3);
numbers.add(1);
numbers.add(4);
numbers.add(1);
numbers.add(5);
System.out.println("Array list with duplicates");
System.out.println(numbers);
LinkedHashSet<Integer> uniqueNumbers = new LinkedHashSet<>(numbers);
ArrayList<Integer> uniqueList = new ArrayList<>(uniqueNumbers);
System.out.println("Array list without duplicates");
System.out.println(uniqueList);
}
}

In the code above, we:

  • Created an ArrayList numbers with duplicate values.

  • Created a new LinkedHashSet from the ArrayList. This will:

    • Remove the duplicate elements.
    • Maintain the List order.
  • Created an ArrayList from the LinkedHashSet that only contains the unique values.

2. Using the distinct() method of the Stream API

The distinct() method of the Stream API returns a stream with distinct elements. The elements are compared through the use of the object’s equals method.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
class RemoveDuplicate {
public static void main( String args[] ) {
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(1);
numbers.add(2);
numbers.add(1);
numbers.add(3);
numbers.add(1);
numbers.add(4);
numbers.add(1);
numbers.add(5);
System.out.println("Array list with duplicates");
System.out.println(numbers);
List<Integer> uniqueNumbers;
uniqueNumbers = numbers
.stream()
.distinct()
.collect(Collectors.toList());
System.out.println("Array list without duplicates");
System.out.println(uniqueNumbers);
}
}

In the code above, we removed the duplicated elements of the numbers ArrayList through the use of the Stream().distinct() method that will return a distinct object stream.

The stream of a distinct object is collected into a List through the collect method. We passed Collectors.toList() as an argument to tell the collect method to create a List from the stream.

Free Resources