public class Test
{
public static void main(String[] args) {
Employee e1=new Employee(1);
Employee e2=new Employee(1);
HashMap<Employee,String> map=new HashMap<Employee,String>();
map.put(e1, "A");
map.put(e1, "B");
map.put(e2, "C");
}
}
class Employee{
private int id;
Employee(int id){
this.id=id;
}
@Override
public int hashCode() {
System.out.println("hascode is ="+this.id);
return this.id;
}
@Override
public boolean equals(Object obj) {
System.out.println("Equals");
return super.equals(obj);
}
}
When I put the same e1 object again, the equals () method will not be called, then how to replace the B value for the e1 key without checking the existing objects on the map? (which, I suppose, is the task of the equality method)
source
share