请让我知道重写equals和hashcode方法是强制性的,即使我使用的密钥是不可变的类。不变的查询
例如我有一个不可变的类Employee,我想在地图中用作键。我是否需要重写equals和hashcode方法?
public class Employee {
private int empId;
private String name;
public Employee(String name, int empId) {
this.empId = empId;
this.name=name;
}
public int getEmpId() {
return empId;
}
public String getName() {
return name;
}
}
感谢, 卡迈勒
是的,你**做**需要重写它们。 –