

Recently i write one post related to remove duplicate object from Array, But in that example i used String Example and String is immutable object and too much feet for any collection. But when we want to remove duplicate custom object at at that time we need to work our little more. because for our object java directly can not find which object are duplicate and which are unique without its references.
i.e When we have Employee, Unique identity is EmployeeId. Based on EmployeeId we can identify that Employee is unique or not. for that we need to override two methods like equals and hashCode.
- equals method is used to identify that two object are same or not.
- hashCode method return unique identity of an object.
Table of Contents
1. Java Remove duplicate from ArrayList using Set:
Employee hr = new Employee(1, "HR"); Employee hrDuplicate = new Employee(1, "HR"); // Duplicate Object Employee teamLeader = new Employee(2, "Team Leader"); List<Employee> list=new ArrayList<>(); list.add(hr); list.add(hrDuplicate); list.add(teamLeader); System.out.println("Employee List(Duplicate)"); for (Employee employee : list) { System.out.println(employee.getEmployeeName()); } HashSet<Employee> hashSet = new HashSet(list); // create has set. Set will contains only unique objects System.out.println("Employee List(Unique)"); for (Employee employee : hashSet) { System.out.println(employee.getEmployeeName()); }
DO NOT FORGOT TO OVERRIDE EQUALS METHOD OF CLASS
Output:
Employee List(Duplicate) HR HR Team Leader Employee List(Unique) HR Team Leader
2. Java Remove duplicate from ArrayList using Stream distinct method
Employee hr = new Employee(1, "HR"); Employee hrDuplicate = new Employee(1, "HR"); // Duplicate Object Employee teamLeader = new Employee(2, "Team Leader"); java.util.List<Employee> list=new ArrayList<>(); list.add(hr); list.add(hrDuplicate); list.add(teamLeader); System.out.println("Employee List(Duplicate)"); for (Employee employee : list) { System.out.println(employee.getEmployeeName()); } java.util.List<Employee> uniqueEmployee = list.stream() .distinct() // it will remove duplicate object, It will check duplicate using equals method .collect(Collectors.toList()); System.out.println("Employee List(Unique)"); for (Employee employee : uniqueEmployee) { System.out.println(employee.getEmployeeName()); }
DO NOT FORGOT TO OVERRIDE EQUALS METHOD OF CLASS
Output:
Employee List(Duplicate) HR HR Team Leader Employee List(Unique) HR Team Leader
Employee.java
/** * Created by Java Developer Zone on 24-03-2017. */ public class Employee { private int employeeId; private String employeeName; public Employee(int employeeId, String employeeName) { this.employeeId = employeeId; this.employeeName = employeeName; } public int getEmployeeId() { return employeeId; } public void setEmployeeId(int employeeId) { this.employeeId = employeeId; } public String getEmployeeName() { return employeeName; } public void setEmployeeName(String employeeName) { this.employeeName = employeeName; } @Override public boolean equals(Object obj) { if (obj instanceof Employee) { return ((Employee) obj).employeeId == employeeId; } return false; } @Override public int hashCode() { return this.employeeId; } }
Was this post helpful?
Let us know if you liked the post. That’s the only way we can improve.