Clement Amarnath Clement Amarnath - 4 months ago 7
Java Question

Why it is not possible to differentiate objects only with hashcode while storing it in Hashset, why it works when I override equals and hashcode

I have a POJO as shown below

public class Employee {

public Employee(int empId, String empName) {
this.id = empId;
this.name = empName;
}
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

@Override
public int hashCode() {
return id;
}

@Override
public boolean equals(Object obj) {
boolean isObjExists = false;
Employee e = (Employee) obj;
if(e.id == this.id)
{
isObjExists = true;
}
return isObjExists;
}
}


This is my main method class

import java.util.HashSet;
import java.util.Set;

public class ExploreSet {

public static void main(String[] args) {
Employee emp1 = new Employee(1, "Clement Amarnath");
Employee emp2 = new Employee(2, "Gowriraj");
Employee emp3 = new Employee(3, "Rockin");
Employee emp4 = new Employee(4, "Inba");
Employee emp5 = new Employee(1, "Clement Amarnath");
Employee emp6 = new Employee(2, "Gowri");

System.out.println(emp1.hashCode() + " " + emp2.hashCode() + " "+emp5.hashCode()+ " " + emp6.hashCode());

Set mySet = new HashSet<Employee>();
mySet.add(emp1);
mySet.add(emp2);
mySet.add(emp3);
mySet.add(emp4);
mySet.add(emp5);
mySet.add(emp6);

System.out.println(mySet.size());
}
}


If I'm commenting the overrided hashcode method in my pojo, I'm getting the set size as 6, which is perfect, since when I instantiate
employee object I will be getting a unique hashcode and all the objects are stored in set.

If I uncomment both the overrided hashcode and overrided equals method, I'm getting the result as expected.


Please clarify why I'm getting count as 6 when I'm uncommenting
overrided hashcode method and commented the overrided equals method. Here the hashcode are duplicated but still I have the size of the set as 6.


I referred this post but I'm not clarified with the answer it - Why do I need to override the equals and hashCode methods in Java?

Answer

Please clarify why I'm getting count as 6 when I'm uncommenting overrided hashcode method and commented the overrided equals method. Here the hashcode are duplicated but still I have the size of the set as 6.

In this case you are generating hash code and commentted equals method.So equals method of Object class get called on the Employee objects you putting in set, and as per Object class equals implementation as :

public boolean equals(Object paramObject)
  {
    return (this == paramObject);
  }

It chacking the references are same or not (which you are putting in Set which are different) that's the reason you got the count 6. This is the expected behavior of Set means it doesn't allowed duplicate as per equals.

Update :

But when I comment hashcode and uncomment my equals still I'm getting the count as 6.

When you comment hashcode and uncomment euqls method again Object class hashcode mehtod will call to generate hashcode for the object and it's different for each object. so naturally if hashcodes are different than object are different hence Set allowed them and you get 6.(as in your question you sysout the hashcode of objects just see the difference by commenting and uncommenting the hashcode method in employee class you will get to know what I mean.)