Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

How Do You Remove Duplicates from a List in Java?

Java
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedDec 4, 2026
5 min read

The simplest way to remove duplicates from a list in Java is to copy it into a LinkedHashSet and back into a list, which drops repeats and keeps the original order. On Java 16 and later, stream().distinct().toList() does the same in one readable line. This guide shows each option so you can remove duplicates list java code in the way that fits your data.

A Set cannot hold equal elements, and that is the idea behind most solutions. Duplicates are decided by equals and hashCode, so custom classes must implement both correctly.

Table of Contents

  • Remove duplicates with LinkedHashSet
  • Remove duplicates with streams
  • Remove duplicates in place
  • Removing duplicates by a field
  • Choosing the right approach
  • Frequently Asked Questions
    • Does converting a list to a HashSet keep the order?
    • How are duplicates detected for my own class?
    • Can I remove duplicates from a list of arrays or nested lists?
    • Is distinct() safe on a parallel stream?
  • Summary
  • Related Articles

Remove duplicates with LinkedHashSet

LinkedHashSet removes duplicates and remembers insertion order, so the first occurrence of each value stays in its original position.

java
text

If order does not matter, new HashSet<>(names) also works, but the resulting order is unspecified. Use new TreeSet<>(names) when you want the result sorted.

Remove duplicates with streams

Stream.distinct() uses equals to drop repeats and keeps the encounter order for ordered streams such as lists.

java
text

toList() returns an unmodifiable list. If you need to change the result later, use .collect(Collectors.toCollection(ArrayList::new)) instead. Before Java 16 you would write .collect(Collectors.toList()).

Remove duplicates in place

To change the existing list instead of creating a new one, track what you have already seen. removeIf with a HashSet does this in one pass.

java
text

Set.add returns false when the element is already present, so !seen.add(tag) is true only for repeats. The predicate has a side effect, which is fine for a single sequential pass but should not be used with parallel streams.

Avoid the nested-loop version that calls list.contains or list.remove for each element. It is O(n squared) and removing inside an index loop easily skips elements.

Removing duplicates by a field

For objects, you often want uniqueness by one field, such as an id or email. distinct() will not help unless equals uses that field. Collect into a map keyed by the field and keep the first value:

java
text

The merge function (first, second) -> first keeps the first user for a repeated id, and LinkedHashMap::new preserves order. A record gets a correct equals and hashCode for free, which is why it works well as a set element.

Choosing the right approach

GoalApproach
Keep first occurrences in orderLinkedHashSet or stream().distinct()
Sorted unique valuesTreeSet
Modify the original listremoveIf with a HashSet
Unique by one fieldCollectors.toMap with a merge function
Order irrelevantHashSet

All set-based approaches run in roughly O(n) time and use extra memory for the set.

Frequently Asked Questions

Does converting a list to a HashSet keep the order?

No. HashSet has no defined order. Use LinkedHashSet to keep insertion order.

How are duplicates detected for my own class?

By equals and hashCode. If you do not override them, two objects are equal only when they are the same instance, so nothing is removed. A record provides both automatically.

Can I remove duplicates from a list of arrays or nested lists?

Arrays use identity equality, so wrap them in a List first (Arrays.asList) or compare with Arrays.equals. Nested lists compare by contents, so distinct() works on them.

Is distinct() safe on a parallel stream?

Yes, but for ordered streams it must preserve order, which can be costly. Use a sequential stream for ordinary lists.

Summary

Use new ArrayList<>(new LinkedHashSet<>(list)) or list.stream().distinct().toList() to remove duplicates while keeping order, removeIf with a HashSet to change a list in place, and Collectors.toMap to dedupe by a field. Make sure equals and hashCode define what a duplicate is.

Related Articles

  • How Do You Use the Arrays Class for Sorting and Searching in Java?
  • What is the PriorityQueue class in Java?
  • Learn Java: Complete Study Roadmap from Beginner to Advanced
Share

Similar posts

  • How Do You Use the Arrays Class for Sorting and Searching in Java?
  • How Do You Iterate Over a Map in Java? Four Ways with Examples
  • How Does HashMap Work Internally in Java? Buckets and Hashing
  • What Is the Difference Between ArrayList and LinkedList in Java?
  • What Are Wildcards in Java Generics? extends vs super
  • How Do You Use EnumMap and EnumSet in Java?
  • What Causes NullPointerException in Java and How Do You Fix It?
  • What Is the Difference Between Error and Exception in Java?