Updated October 2026. Every example was run on Java 21.
The easiest way to remove duplicates from a list in Java is list.stream().distinct().collect(Collectors.toList()). It keeps the first copy of each element and preserves the order. For objects, you must also override equals() and hashCode(), or compare by a single property instead.
Below, you will see five ways to do it. Each one has real output, and each one fits a different situation.
Which way should you use?
| Situation | Best way | Keeps order? |
|---|---|---|
Strings, numbers, or objects with equals() |
stream().distinct() |
Yes |
| Plain collections, no streams | new LinkedHashSet<>(list) |
Yes |
| Duplicates are decided by one field | Filter with a Set of seen keys |
Yes |
| Same, and you want the result as a map | Collectors.toMap() with LinkedHashMap |
Yes |
| Same, and you want the list sorted | TreeSet with a Comparator |
No, it sorts |
| Change the existing list in place | removeIf() |
Yes |
1. Use stream().distinct()
For strings, numbers and other types that already have a good equals(), distinct() does everything for you.
import java.util.List;
import java.util.stream.Collectors;
public class Distinct {
public static void main(String[] args) {
List<String> names = List.of("IT", "HR", "IT", "Development", "HR");
List<String> unique = names.stream()
.distinct()
.collect(Collectors.toList());
System.out.println(unique);
}
}
Output:
[IT, HR, Development]
The first IT and the first HR stay. Also, the order is the same as in the original list.
2. distinct() on your own objects
Things get interesting when the list holds your own class. First, here is a Department class without equals() and hashCode().
WRONG
import java.util.List;
import java.util.stream.Collectors;
public class DistinctNoEquals {
static class Department {
final int id;
final String name;
Department(int id, String name) { this.id = id; this.name = name; }
@Override public String toString() { return id + " : " + name; }
}
public static void main(String[] args) {
List<Department> departments = List.of(
new Department(1, "IT"), new Department(2, "HR"),
new Department(1, "IT"));
System.out.println("Before: " + departments.size());
System.out.println("After distinct(): "
+ departments.stream().distinct().collect(Collectors.toList()).size());
}
}
Output:
Before: 3
After distinct(): 3
Nothing was removed. That is because, without equals(), Java compares object references, and two new Department(1, "IT") calls create two different objects.
The fix is to define equality. Fortunately, since Java 16, a record does this for you.
RIGHT
import java.util.List;
import java.util.stream.Collectors;
public class DistinctObjects {
// A record gets equals() and hashCode() for free (Java 16+)
record Department(int id, String name) { }
public static void main(String[] args) {
List<Department> departments = List.of(
new Department(1, "IT"), new Department(2, "HR"),
new Department(1, "IT"), new Department(4, "Development"),
new Department(2, "HR"));
departments.stream().distinct().collect(Collectors.toList())
.forEach(System.out::println);
}
}
Output:
Department[id=1, name=IT]
Department[id=2, name=HR]
Department[id=4, name=Development]
On Java 8, write a normal class and generate equals() and hashCode() from your IDE. Both methods must use the same fields.
3. Use a LinkedHashSet
A Set never holds duplicates, so copying a list into one removes them. However, the choice of Set decides the order of the result:
import java.util.*;
import java.util.stream.Collectors;
public class Order {
public static void main(String[] args) {
List<String> names = Arrays.asList("IT", "HR", "IT", "Development", "HR");
System.out.println("HashSet : " + new ArrayList<>(new HashSet<>(names)));
System.out.println("LinkedHashSet : " + new ArrayList<>(new LinkedHashSet<>(names)));
System.out.println("TreeSet : " + new ArrayList<>(new TreeSet<>(names)));
System.out.println("distinct() : " + names.stream().distinct().collect(Collectors.toList()));
}
}
Output:
HashSet : [Development, HR, IT]
LinkedHashSet : [IT, HR, Development]
TreeSet : [Development, HR, IT]
distinct() : [IT, HR, Development]
LinkedHashSet keeps the original order, and TreeSet sorts the result. HashSet makes no promise about order at all. It happened to look sorted here, but you should not rely on that. For a detailed comparison, see HashSet vs TreeSet in Java.
4. Remove duplicates from a list in Java by one property
Sometimes two objects are “the same” only because one field matches. For example, you may want one department for each name, whatever its id. Here are four ways to do that with the same data. The input is five departments, and IT and HR each appear twice with different ids.
import java.util.*;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
public class ByProperty {
record Department(int id, String name) { }
// keeps the first element for each key
static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
Set<Object> seen = new HashSet<>();
return t -> seen.add(keyExtractor.apply(t));
}
public static void main(String[] args) {
List<Department> list = List.of(
new Department(1, "IT"), new Department(2, "HR"),
new Department(3, "IT"), new Department(4, "Development"),
new Department(5, "HR"));
System.out.println("-- filter with a Set (keeps order, first wins)");
list.stream().filter(distinctByKey(Department::name)).forEach(System.out::println);
System.out.println("-- toMap with LinkedHashMap");
new LinkedHashMap<>(list.stream().collect(Collectors.toMap(
Department::name, d -> d, (first, second) -> first, LinkedHashMap::new)))
.values().forEach(System.out::println);
System.out.println("-- TreeSet with comparator (sorted by name)");
list.stream().collect(Collectors.toCollection(
() -> new TreeSet<>(Comparator.comparing(Department::name))))
.forEach(System.out::println);
System.out.println("-- removeIf on a mutable list");
List<Department> mutable = new ArrayList<>(list);
Set<String> seen = new HashSet<>();
mutable.removeIf(d -> !seen.add(d.name()));
mutable.forEach(System.out::println);
}
}
Output:
-- filter with a Set (keeps order, first wins)
Department[id=1, name=IT]
Department[id=2, name=HR]
Department[id=4, name=Development]
-- toMap with LinkedHashMap
Department[id=1, name=IT]
Department[id=2, name=HR]
Department[id=4, name=Development]
-- TreeSet with comparator (sorted by name)
Department[id=4, name=Development]
Department[id=2, name=HR]
Department[id=1, name=IT]
-- removeIf on a mutable list
Department[id=1, name=IT]
Department[id=2, name=HR]
Department[id=4, name=Development]
All four keep the first department for each name. However, the TreeSet version is the only one that also sorts, so choose it only when you want sorted output.
Here is how each one works:
filter()with aSetrelies onSet.add()returningfalsefor a key it has already seen. That makes it a one-line “have I seen this before?” check.toMap()uses the name as the key. The merge function(first, second) -> firstdecides what happens on a clash, andLinkedHashMap::newkeeps the order.TreeSettreats two objects as equal when the comparator returns 0. So the comparator doubles as your definition of “duplicate”.removeIf()changes the list itself instead of making a new one.
Be careful with the first one. Because the distinctByKey predicate holds state, use it only on a sequential stream. Otherwise, on a parallel stream, the result is not guaranteed.
5. removeIf() fails on an immutable list
removeIf() changes the list in place. As a result, it fails on lists made with List.of() or Stream.toList(), because those lists cannot be modified.
import java.util.List;
public class Immutable {
public static void main(String[] args) {
List<String> names = List.of("IT", "HR", "IT");
try {
names.removeIf(n -> n.equals("IT"));
} catch (UnsupportedOperationException e) {
System.out.println("UnsupportedOperationException: List.of() cannot be modified");
}
}
}
Output:
UnsupportedOperationException: List.of() cannot be modified
If you are not sure that the list is mutable, copy it first with new ArrayList<>(list), or use one of the stream versions above. They never touch the original.
Common mistakes
- Using
distinct()on objects withoutequals()andhashCode(). Nothing is removed, because Java compares references. - Overriding
equals()but nothashCode(). Otherwise, hash-based sets anddistinct()give wrong results. Always override both together. - Relying on
HashSetorder. The order can change between Java versions. UseLinkedHashSetwhen order matters. - Calling
removeIf()onList.of(). It throwsUnsupportedOperationException. - Using raw types like
List deptList = new ArrayList();. You lose type safety, and the lambdas stop compiling cleanly. Instead, always writeList<Department>.
Summary
For most lists, stream().distinct() is all you need. When your objects have no equals(), or when only one field defines a duplicate, use a Set of seen keys, toMap(), or a TreeSet with a comparator. In short, pick the version that matches the order you want and whether you may change the original list.
References
Related posts
- Difference between HashSet vs TreeSet in Java
- Java8 map() vs reduce()
- How to handle Null Values in Java8 Streams