我们将一个String键存储在HashMap中,该HashMap是三个String字段和一个布尔字段的串联.问题是如果分隔符出现在字段值中,则可以创建重复键.
所以为了解决这个问题,根据另一篇文章中的建议,我打算创建一个将用作HashMap键的键类:
class TheKey {
public final String k1;
public final String k2;
public final String k3;
public final boolean k4;
public TheKey(String k1, String k2, String k3, boolean k4) {
this.k1 = k1; this.k2 = k2; this.k3 = k3; this.k4 = k4;
}
public boolean equals(Object o) {
TheKey other = (TheKey) o;
//return true if all four fields are equal
}
public int hashCode() {
return ???;
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是: