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:
- Set collection
- The
distinctmethod of the Stream API
1. Using a set to remove duplicate elements
We can use a set
In the code below, we will use LinkedHashSet
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
numberswith duplicate values. -
Created a new
LinkedHashSetfrom the ArrayList. This will:- Remove the duplicate elements.
- Maintain the List order.
-
Created an ArrayList from the
LinkedHashSetthat 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.