Remove duplicates from a list of objects based on property in Java

Remove duplicates from a list of objects based on property in Java

To remove duplicates from a list of objects based on a specific property in Java, you can follow these steps:

  1. Create a custom equals() and hashCode() method for your object class to determine equality based on the property you want to use for comparison.

  2. Use a Set to filter out duplicates.

Here's an example illustrating how to remove duplicates from a list of objects based on a property:

Suppose you have a Person class with a name property, and you want to remove duplicate Person objects based on their name property:

import java.util.*; class Person { private String name; public Person(String name) { this.name = name; } public String getName() { return name; } // Custom equals() and hashCode() based on 'name' property @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Person person = (Person) o; return Objects.equals(name, person.name); } @Override public int hashCode() { return Objects.hash(name); } } public class RemoveDuplicatesExample { public static void main(String[] args) { // Create a list of Person objects List<Person> personList = new ArrayList<>(); personList.add(new Person("Alice")); personList.add(new Person("Bob")); personList.add(new Person("Alice")); // Duplicate personList.add(new Person("Charlie")); personList.add(new Person("Bob")); // Duplicate // Remove duplicates based on the 'name' property List<Person> uniquePersonList = removeDuplicates(personList); // Print the unique Person objects for (Person person : uniquePersonList) { System.out.println(person.getName()); } } // Function to remove duplicates based on the 'name' property public static List<Person> removeDuplicates(List<Person> inputList) { Set<Person> uniquePersons = new HashSet<>(); List<Person> result = new ArrayList<>(); for (Person person : inputList) { if (uniquePersons.add(person)) { // If the person is added to the set, it's unique, so add it to the result list result.add(person); } } return result; } } 

In this example:

  • The Person class has a custom equals() and hashCode() method that considers only the name property for equality and hashing.

  • The removeDuplicates method takes a list of Person objects and uses a HashSet (uniquePersons) to keep track of unique Person objects based on their name property. The result is stored in the result list.

  • By iterating through the input list and adding each Person to the uniquePersons set, duplicates are automatically removed.

  • The result list contains the unique Person objects.

When you run this code, it will remove duplicate Person objects based on their name property, and you'll get a list of unique Person objects as the output.


More Tags

country-codes office-fabric redis mu-law hmvc backtracking serialization clone apache-mina javascript-objects

More Java Questions

More Other animals Calculators

More Chemical thermodynamics Calculators

More Statistics Calculators

More Tax and Salary Calculators