所以我有很多自定义类,它们内部也有使用组合的自定义类。
我的自定义类的变量变化非常频繁,我将它们添加到 HashSets。所以我的问题是当我实现 hashCode 时——对于只有不断变化的私有字段的类,我应该怎么做?
这是一个自定义类的示例:
public class Cell {
protected boolean isActive;
protected boolean wasActive;
public Cell() {
this.isActive = false;
this.wasActive = false;
}
// getter and setter methods...
@Override
public int hashCode() {
// HOW SHOULD I IMPLEMENT THIS IF THIS custom object is constantly
// being added into HashSets and have it's private fields isActive
// and wasActive constantly changed.
}
// ANOTHER QUESTION Am I missing anything with this below equals implementation?
@Override
public boolean equals(Object object) {
boolean result = false;
if (object instanceof Cell) {
Cell otherCell = (Cell) object;
result = (this.isActive == otherCell.isActive && this.wasActive ==
otherCell.wasActive);
}
return result;
}