我对下面的代码感到困惑。当我更改员工e2的名称时,我希望地图同时具有e2和e3对象,因为e2对象现在不同于e3。但是似乎更改e2对象的名称并没有反映在哈希图中存储的e2对象中,因此输出仅显示e3。所以我很困惑哈希表是存储实际对象还是仅存储原始对象的副本。
代码:
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
HashMap<Employee,Integer> map = new HashMap<Employee,Integer>();
Employee e1 = new Employee(10,"Sachin");
Employee e2 = new Employee(10,"Sachin");
map.put(e1, 1);
map.put(e2,2);
System.out.println(e1.hashCode()+" "+e2.hashCode());
System.out.println(map);
e2.setName("Akshay"); //<---- changing the name for e2
Employee e3 = new Employee(10,"Sachin");
map.put(e3, 3);
System.out.println(map);
}
}
class Employee
{
private int id;
private String name;
public Employee(int id, String name) {
super();
this.id = id;
this.name = 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() {
final int prime = 31;
int result = 1;
result = prime * result + id;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Employee other = (Employee) obj;
if (id != other.id)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
输出:
-1826112337 -1826112337
{Employee [id=10, name=Sachin]=2}
{Employee [id=10, name=Sachin]=3}
答案 0 :(得分:0)
您可能已经知道,HashMap中的每个条目都基于其hashCode()分配了一个存储桶。当条目在Map中放入put()时,便会分配存储桶(将项目添加到HashSet也是一样)
如果在放入Map后对密钥进行了变异,则HashMap将不知道该变异。不会重新计算存储桶并将条目移到新存储桶(怎么办?它无法监听此事件)
根据经验,请勿在Map(或Set中的项)中更改键,因为Map.containsKey(...)和Set.contains(... )等