In this post, we will see how to remove duplicates from a List in Java without destroying the original ordering of the elements in the List.
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 will get a set of elements from the list but without any duplicates. Now if we convert the set back to a list, we will get a list without any duplicates.
Please note that the ordering of the elements will be destroyed if an ArrayList
is used. To preserve the original order, we can use TreeSet
or LinkedHashset
instead.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import java.util.*; // Program to remove duplicates from a List in Java class Duplicates { 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 TreeSet<>(listWithDuplicates); // construct a new list from set and print it List<String> listWithoutDuplicates = new ArrayList<>(set); System.out.println(listWithoutDuplicates); } } |
Output:
[C++, Java]
2. Java 8 Streams
Here’s how this can be done in Java 8 using Stream API:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; // Program to remove duplicates from a List in Java 8 class Duplicates { 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 constucted 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++]
Above approach destroys the ordering of the elements in the list. 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 21 22 |
import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; // Program to remove duplicates from a List in Java 8 class Duplicates { 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 Streams List<String> listWithoutDuplicates = listWithDuplicates.stream() .distinct() .collect(Collectors.toList()); System.out.println(listWithoutDuplicates); } } |
Output:
[C++, Java]
3. 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 22 23 24 |
import com.google.common.collect.Lists; import com.google.common.collect.Sets; import java.util.ArrayList; import java.util.Arrays; import java.util.List; // Program to remove duplicates from a List using Guava class Duplicates { 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)); |
Above approach also destroys the ordering of the elements in the list. 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 21 22 23 24 |
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import java.util.ArrayList; import java.util.Arrays; import java.util.List; // Program to remove duplicates from a List using Guava class Duplicates { 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]
Thanks for reading.
Please use ideone or C++ Shell or any other online compiler link to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂
Leave a Reply