Popular Tutorials
Start Learning JavaCertification Courses
Created with over a decade of experience and thousands of feedback.
Java Program to Remove duplicate elements from ArrayList
In this example, we will learn to convert the duplicate element from the ArrayList in Java.
To understand this example, you should have the knowledge of the following Java programming topics:
Example 1: Remove duplicate elements from ArrayList using Set
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Set;
class Main {
public static void main(String[] args) {
// create an arraylist from the array
// using asList() method of the Arrays class
ArrayList<Integer> numbers = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 1, 3));
System.out.println("ArrayList with duplicate elements: " + numbers);
// convert the arraylist into a set
Set<Integer> set = new LinkedHashSet<>();
set.addAll(numbers);
// delete al elements of arraylist
numbers.clear();
// add element from set to arraylist
numbers.addAll(set);
System.out.println("ArrayList without duplicate elements: " + numbers);
}
}
Output
ArrayList with duplicate elements: [1, 2, 3, 4, 1, 3] ArrayList without duplicate elements: [1, 2, 3, 4]
In the above example, we have created an arraylist named numbers. The arraylist contains duplicate elements.
To remove duplicate elements from the arraylist, we have
- add all elements from arraylist to set
- empty the arraylist using clear() method
- add all elements from set to arraylist
Here, we have used the LinkedHashSet to create a set. It is because it removes the duplicate elements and maintains insertion order. To learn more, visit Java LinkedHashSet.
Example 2: Remove duplicate elements from ArrayList using Stream
import java.util.ArrayList;
import java.util.Arrays;
import java.util.stream.Collectors;
import java.util.stream.Stream;
class Main {
public static void main(String[] args) {
// create an arraylist from the array
// using asList() method of the Arrays class
ArrayList<Integer> numbers = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 1, 3));
System.out.println("ArrayList with duplicate elements: " + numbers);
// create a stream from arraylist
Stream<Integer> stream = numbers.stream();
// call the distinct() of Stream
// to remove duplicate elements
stream = stream.distinct();
// convert the stream to arraylist
numbers = (ArrayList<Integer>)stream.collect(Collectors.toList());
System.out.println("ArrayList without duplicate elements: " + numbers);
}
}
Output
ArrayList with duplicate elements: [1, 2, 3, 4, 1, 3] ArrayList without duplicate elements: [1, 2, 3, 4]
In the above example, we have created an arraylist named numbers. The arraylist contains duplicate elements.
Here, we have used the Stream class to remove duplicate elements from the arraylist.
- numbers.stream() - create a stream from the arraylist
- stream.distinct() - removes duplicate elements
- stream.collect(Collectors.toList()) - returns a list from the stream
Here, we have used typecasting to convert the returned list into an arraylist.