Posts

Showing posts with the label Remove duplicates from a list of objects based on a property

Java 8 Stream - Remove duplicates from a list of objects based on a property

Hello everyone, here we will show you how to remove duplicates from a list of objects based on a single property.  In this example, we are getting the stream from the list and putting it in the TreeSet from which we provide a custom comparator that compares id, email, or salary uniquely. Here we are going to remove duplicates based on the, id(Long) property of the user email(String) property of the user salary(Double) property of the user User.java public class User { private Long id; private String name; private String email; private String phone; private Double salary; public Long getId() { return id; } public void setId( Long id) { this .id = id; } public String getName() { return name; } public void setName( String name) { this .name = name; } public String getEmail() { return email; } public void setEmail( String email) { this .email = email