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);
}
}
Run Code Online (Sandbox Code Playgroud)
当我再次放置相同的对象e1时,不会调用equals()方法,那么如何在没有在地图的现有对象中检查的情况下为键e1替换值B?(我认为这是equals方法的工作)