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.
LinkedHashSet removes duplicates and remembers insertion order, so the first occurrence of each value stays in its original position.
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.
Stream.distinct() uses equals to drop repeats and keeps the encounter order for ordered streams such as lists.
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()).
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.
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.
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:
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.
| Goal | Approach |
|---|---|
| Keep first occurrences in order | LinkedHashSet or stream().distinct() |
| Sorted unique values | TreeSet |
| Modify the original list | removeIf with a HashSet |
| Unique by one field | Collectors.toMap with a merge function |
| Order irrelevant | HashSet |
All set-based approaches run in roughly O(n) time and use extra memory for the set.
No. HashSet has no defined order. Use LinkedHashSet to keep insertion order.
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.
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.
Yes, but for ordered streams it must preserve order, which can be costly. Use a sequential stream for ordinary lists.
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.