I just announced the new Spring 5 modules in REST With Spring:

>> CHECK OUT THE COURSE

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.

2. Remove duplicates from a List using Guava

The same can be done using Guava as well:

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.

I just announced the new Spring 5 modules in REST With Spring:

>> CHECK OUT THE LESSONS

Sort by:   newest | oldest | most voted
ChangWonSon
Guest

hi. I have question.
I can’t understand that 3
‘~ based on the result returned by equals() method’ sentence meaning.
equals() method is right?
could you explain more?

Eugen Paraschiv
Guest

It just means that – if the list contains any custom objects (not just Integer like this example) – equals needs to be properly implemented.

Lajos Incze
Guest

does the hashset method preserve the order of the elements?

Eugen Paraschiv
Guest

No it does not. You’re probably looking for a TreeSet. Cheers,
Eugen.

Big Kahuna
Guest

or a LinkedHashset to preserve list order rather than comparator order