We store the string key in the HashMap, which is the concatenation of the three String fields and the logical field. The problem is that duplicate keys can be created if the delimiter is displayed in the field value.
So, to get around this, based on tips in another , I plan to create a key class that will be used as the HashMap key:
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;
}
public int hashCode() {
return ???;
}
}
My questions:
- What value should be returned from hashCode (). The map will contain a total of about 30 values. Out of these 30, there are about 10 different k1 values (some entries have the same k1 value).
- To save this key class as a HashMap key, you only need to override the equals () and hashCode () methods? Is anything else needed?