In this shot, we will learn two common ways to remove duplicate elements from an ArrayList
. The methods involve the use of:
distinct
method of the Stream APIset
to remove duplicate elementsWe 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 numbers
with duplicate values.
Created a new LinkedHashSet
from the ArrayList. This will:
Created an ArrayList from the LinkedHashSet
that only contains the unique values.
distinct()
method of the Stream APIThe 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.
RELATED TAGS
CONTRIBUTOR
View all Courses