Remove duplicates from a List in Java
This post will discuss how to remove duplicates from a list in Java without destroying the original ordering of the list elements.
1. Plain Java
We know that a set doesn’t allow any duplicate elements. So if we convert the given list with duplicates to a set, we’ll get a set of elements from the list without duplicates. If we convert the set back to a list, we’ll get a list without duplicates.
Please note that HashSet will destroy the ordering of the elements. To preserve the original order, we can use LinkedHashset instead.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.util.*; class Main { public static void main(String[] args) { // input list with duplicates List<String> listWithDuplicates = new ArrayList<>(Arrays.asList("C++", "Java", "C++")); // construct a set from elements of the list Set<String> set = new LinkedHashSet<>(listWithDuplicates); // construct a new list from a set and print it List<String> listWithoutDuplicates = new ArrayList<>(set); System.out.println(listWithoutDuplicates); } } |
Output:
[C++, Java]
2. Using Java 8 Stream
Here’s how we can do this in Java 8 and above using Stream:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; class Main { public static void main(String[] args) { // input list with duplicates List<String> listWithDuplicates = new ArrayList<>(Arrays.asList("C++", "Java", "C++")); // Construct a new list from the set constructed from elements // of the original list List<String> listWithoutDuplicates = listWithDuplicates.stream() .collect(Collectors.toSet()) .stream() .collect(Collectors.toList()); System.out.println(listWithoutDuplicates); } } |
Output:
[Java, C++]
The above approach destroys the ordering of the list elements. To preserve the original order, we can use Stream.distinct(), as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; class Main { public static void main(String[] args) { // input list with duplicates List<String> listWithDuplicates = new ArrayList<>(Arrays.asList("C++", "Java", "C++")); // Removing duplicates from the list using Java 8 Stream List<String> listWithoutDuplicates = listWithDuplicates.stream() .distinct() .collect(Collectors.toList()); System.out.println(listWithoutDuplicates); } } |
Output:
[C++, Java]
This is equivalent to:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.util.*; class Main { public static void main(String[] args) { // input list with duplicates List<String> listWithDuplicates = new ArrayList<>(Arrays.asList("C++", "Java", "C++")); List<String> listWithoutDuplicates = new ArrayList<>(); Set<String> uniqueValues = new HashSet<>(); for (String listWithDuplicate : listWithDuplicates) { if (uniqueValues.add(listWithDuplicate)) { listWithoutDuplicates.add(listWithDuplicate); } } System.out.println(listWithoutDuplicates); } } |
Output:
[C++, Java]
3. Using Guava Library
Guava also provides static methods to create mutable instances of List and Set. These can be used with Java 7 or before to remove duplicates from a list.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import com.google.common.collect.Lists; import com.google.common.collect.Sets; import java.util.ArrayList; import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { // input list containing duplicates List<String> listWithDuplicates = new ArrayList<>(Arrays.asList("C++", "Java", "C++")); // Using Guava – mutable list List<String> listWithoutDuplicates = Lists.newArrayList(Sets.newHashSet(listWithDuplicates)); System.out.println(listWithoutDuplicates); } } |
Output:
[Java, C++]
If mutability is not required, we can go for immutable lists:
|
1 |
listWithoutDuplicates = ImmutableList.copyOf(ImmutableSet.copyOf(listWithDuplicates)); |
The above approach also destroys the ordering of the list elements. To preserve the original order, we can use ImmutableSet.copyOf(), as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import java.util.ArrayList; import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { // input list containing duplicates List<String> listWithDuplicates = new ArrayList<>(Arrays.asList("C++", "Java", "C++")); // Using Guava – Immutable list List<String> listWithoutDuplicates = ImmutableSet.copyOf(listWithDuplicates).asList(); System.out.println(listWithoutDuplicates); } } |
Output:
[C++, Java]
That’s all about removing duplicates from a List in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)