Learnitweb

Remove Duplicates from a List by Field in Java

Assume an Employee class with an id field:

public class Employee {
    private int id;
    private String name;
    // constructor, getters
}

Approach 1: Using toMap() (Keeps first occurrence)

Collection<Employee> unique = employees.stream()
    .collect(Collectors.toMap(
        Employee::getId,      // key
        e -> e,               // value
        (e1, e2) -> e1,       // merge: keep first
        LinkedHashMap::new    // preserve order
    ))
    .values();

Approach 2: Using TreeSet with Comparator (Keeps first per comparator)

List<Employee> unique = employees.stream()
    .collect(Collectors.collectingAndThen(
        Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Employee::getId))),
        ArrayList::new
    ));

Summary:

  • toMap() deduplicates by key and preserves insertion order.
  • TreeSet deduplicates using a comparator and can enforce ordering.