I have created an Employee class having name and employeeNo . I am overriding equals and hashcode method.
@Override
public boolean equals(Object obj) {
final Employee other = (Employee) obj;
if (this.employeeNo == other.employeeNo && this.name.equals(other.name)) {
return true;
}
return false;
}
@Override
public int hashCode() {
int hash = 3;
hash = 53 * hash + (this.name != null ? this.name.hashCode() : 0);
hash = 53 * hash + this.employeeNo;
return hash;
}
My test class
Employee p = new Employee();
p.setName("v");
p.setEmployeeNo(1);
Employee p1 = new Employee();
p.setName("v");
p.setEmployeeNo(1);
System.out.println(p==p1); // Print false . why?