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

In this section, 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
+ "}";
}
}


MainApplication.java

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.TreeSet;

import static java.util.Comparator.*;
import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toCollection;

public class MainApplication {

public static void main(String[] args) {

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");

}
}

1. Java's built-in Arrays.asList() method converts an array into a list. This method takes an array as an argument and returns a List object . The object returned by the Arrays.asList() method is a fixed-size list, which means we cannot add or remove elements from it. 

2. Java List interface provides stream() method which returns a sequential Stream with list of User as its source here.

3. collect() is one of Stream API the Java 8 terminal methods. This allows us to perform mutable folding operations (repack the elements into some data structures and apply some additional logic, concatenate them, etc.) on the data elements stored in the instance Stream. 

4. collectingAndThen() is a special collector that allows us to perform another action on the result immediately after the collection is complete. Here collected the elements Stream into a TreeSet instance and then convert the result to an instance ArrayList.

5. TreeSet allows unique elements.

6. comparing(), comparingLong(), and comparingDouble() methods are the part of java Comparator interface.


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


More interesting topics,

Spring Boot | GCP 

Test Slices in Spring-Boot


Integration Testing

Popular posts from this blog

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

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

ReactJS, Spring Boot JWT Authentication Example

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete