java-remove-duplicates-from-list
Removing all duplicates from a List in Java
This quick tutorial is going to show you how to clean up the duplicate elements from a List – first using plain Java, then Guava and finally a Java 8 Lambda-based solution.
This article is part of the “Java – Back to Basic” series here on Baeldung.
1. Remove Duplicates from a List Using Plain Java
Removing the duplicate elements from a List with the standard Java Collections Framework is done easily through a Set:
public void
givenListContainsDuplicates_whenRemovingDuplicatesWithPlainJava_thenCorrect() {
List<Integer> listWithDuplicates = Lists.newArrayList(0, 1, 2, 3, 0, 0);
List<Integer> listWithoutDuplicates = new ArrayList<>(
new HashSet<>(listWithDuplicates));
assertThat(listWithoutDuplicates, hasSize(4));
}
As you can see, the original list remains unchanged.
Further reading:
Java Collections Interview Questions
A set of practical Collections-related Java interview questions
Java – Combine Multiple Collections
A quick and practical guide to combining multiple collections in Java
How to Find an Element in a List with Java
Have a look at some quick ways to find an element in a list in Java
2. Remove Duplicates from a List Using Guava
public void
givenListContainsDuplicates_whenRemovingDuplicatesWithGuava_thenCorrect() {
List<Integer> listWithDuplicates = Lists.newArrayList(0, 1, 2, 3, 0, 0);
List<Integer> listWithoutDuplicates
= Lists.newArrayList(Sets.newHashSet(listWithDuplicates));
assertThat(listWithoutDuplicates, hasSize(4));
}
And again, the original list remains unchanged.
3. Remove Duplicates from a List Using Java 8 Lambdas
Finally – let’s look at a new solution, using Lambdas in Java 8; we’re going to use the distinct() method from the Stream API which returns a stream consisting of distinct elements based on the result returned by equals() method:
public void
givenListContainsDuplicates_whenRemovingDuplicatesWithJava8_thenCorrect() {
List<Integer> listWithDuplicates = Lists.newArrayList(1, 1, 2, 2, 3, 3);
List<Integer> listWithoutDuplicates = listWithDuplicates.stream()
.distinct()
.collect(Collectors.toList());
}
And there we have it – 3 quick ways to clean up all the duplicate items from a List.
4. Conclusion
This article demonstrates us how easy we can Remove Duplicates from a List Using Plain Java, Google Guava and Java 8.
The implementation of all of these examples and snippets can be found in the GitHub project. This is a Maven-based project so it should be easy to import and run.