Java Top

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

>> 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.

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

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.

Java bottom

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

>> CHECK OUT THE COURSE
5 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
ChangWonSon
ChangWonSon
5 years ago

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
5 years ago
Reply to  ChangWonSon

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
Lajos Incze
4 years ago

does the hashset method preserve the order of the elements?

Eugen Paraschiv
4 years ago
Reply to  Lajos Incze

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

Big Kahuna
Big Kahuna
4 years ago

or a LinkedHashset to preserve list order rather than comparator order

Comments are closed on this article!