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;
}

public String getPhone() {
return phone;
}

public void setPhone(String phone) {
this.phone = phone;
}

public Double getSalary() {
return salary;
}

public void setSalary(Double salary) {
this.salary = salary;
}

public User(Long id, String name, String email,
String phone, Double salary) {
super();
this.id = id;
this.name = name;
this.email = email;
this.phone = phone;
this.salary = salary;
}
@Override
public String toString() {
return "User {id=" + id + ", name=" + name
+ ", email=" + email + ", phone="
+ phone + ", salary=" + salary
+ "}";
}
}


DemoApplication.java

import static java.util.Comparator.comparing;
import static java.util.Comparator.comparingDouble;
import static java.util.Comparator.comparingLong;
import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toCollection;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.TreeSet;

public class DemoApplication {

public static void main(String[] args) throws IOException {

List<User> users = Arrays.asList(new User(1l, "alpha", "alpha@email.sl", "+00-8976", 1000d),
new User(2l, "beta", "alpha@email.sl", "+300-5458976", 1500d),
new User(3l, "gama", "gama@email.sl", "+00-538976", 600d),
new User(3l, "peca", "peca@email.sl", "+500-538976", 400d),
new User(4l, "star", "star@email.sl", "+900-538976", 400d));

System.out.println("Remove duplicates based on String(email) property of user");
List<User> usersDistinctByEmail = users.stream().collect(
collectingAndThen(toCollection(() ->
new TreeSet<>(comparing(User::getEmail))), ArrayList::new));
System.out.println("Result = " + usersDistinctByEmail + "\n");

System.out.println("Remove duplicates based on Long(id) property of user");
List<User> usersDistinctById = users.stream().collect(
collectingAndThen(toCollection(() ->
new TreeSet<>(comparingLong(User::getId))), ArrayList::new));
System.out.println("Result = " + usersDistinctById + "\n");

System.out.println("Remove duplicates based on Double(Salary) property of user");
List<User> usersDistinctBySalary = users.stream().collect(
collectingAndThen(toCollection(() ->
new TreeSet<>(comparingDouble(User::getSalary))), ArrayList::new));
System.out.println("Result = " + usersDistinctBySalary + "\n");

}
}


Console O/P

Remove duplicates based on String(email) property of user
Result = [User {id=1, name=alpha, email=alpha@email.sl, phone=+00-8976, salary=1000.0}, User {id=3, name=gama, email=gama@email.sl, phone=+00-538976, salary=600.0}, User {id=3, name=peca, email=peca@email.sl, phone=+500-538976, salary=400.0}, User {id=4, name=star, email=star@email.sl, phone=+900-538976, salary=400.0}]

Remove duplicates based on Long(id)  property of user
Result = [User {id=1, name=alpha, email=alpha@email.sl, phone=+00-8976, salary=1000.0}, User {id=2, name=beta, email=alpha@email.sl, phone=+300-5458976, salary=1500.0}, User {id=3, name=gama, email=gama@email.sl, phone=+00-538976, salary=600.0}, User {id=4, name=star, email=star@email.sl, phone=+900-538976, salary=400.0}]

Remove duplicates based on Double(Salary)  property of user
Result = [User {id=3, name=peca, email=peca@email.sl, phone=+500-538976, salary=400.0}, User {id=3, name=gama, email=gama@email.sl, phone=+00-538976, salary=600.0}, User {id=1, name=alpha, email=alpha@email.sl, phone=+00-8976, salary=1000.0}, User {id=2, name=beta, email=alpha@email.sl, phone=+300-5458976, salary=1500.0}]


Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Spring boot video streaming example-HTML5

Spring Boot + Mockito simple application with 100% code coverage

Spring Boot + OpenCSV Export Data to CSV Example

Custom Exception Handling in Quarkus REST API

Registration and Login with Spring Boot + Spring Security + Thymeleaf

DataTable-Pagination example with Spring boot, jQuery and ajax

Spring Webflux File Download - REST API Example

Node JS mini projects with source code - free download

ReactJS, Spring Boot JWT Authentication Example