Compound String Key in HashMap

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;
      //return true if all four fields are equal
  }

  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?
+2
9

hashCode equals . - :

public int hashCode() {
  int hash = 17;
  hash = hash * 31 + k1.hashCode();
  hash = hash * 31 + k2.hashCode();
  hash = hash * 31 + k3.hashCode();
  hash = hash * 31 + k4 ? 0 : 1;
  return hash;
}

, , . 0 "" - . /-, :

public static boolean equals(Object o1, Object o2) {
  if (o1 == o2) {
    return true;
  }
  if (o1 == null || o2 == null) {
    return false;
  }
  return o1.equals(o2);
}

public static boolean hashCode(Object o) {
  return o == null ? 0 : o.hashCode();
}

- , - :

public int hashCode() {
  int hash = 17;
  hash = hash * 31 + ObjectUtil.hashCode(k1);
  hash = hash * 31 + ObjectUtil.hashCode(k2);
  hash = hash * 31 + ObjectUtil.hashCode(k3);
  hash = hash * 31 + k4 ? 0 : 1;
  return hash;
}
+12

Eclipse hashCode Alt-Shift-S h.

+10

hashCode() , . - ( int), , :

  • - , . , -, - , , , , , . hashCode(), , , , String hashCode() .
    , , , 90- , (, , ) hashCode().

  • equals(). equals() , . , -. false ( ), .

, .

Edit: ... : " , , ". ? + + + +, , , , , , , ( ).

, ... , , . .

+2

Eclipse 3.5 hashcode equals:)

+2

ans hashCode : ( intellij idea, null)

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;
}

@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;

    TheKey theKey = (TheKey) o;

    if (k4 != theKey.k4) return false;
    if (k1 != null ? !k1.equals(theKey.k1) : theKey.k1 != null) return false;
    if (k2 != null ? !k2.equals(theKey.k2) : theKey.k2 != null) return false;
    if (k3 != null ? !k3.equals(theKey.k3) : theKey.k3 != null) return false;

    return true;
}

@Override
public int hashCode() {
    int result = k1 != null ? k1.hashCode() : 0;
    result = 31 * result + (k2 != null ? k2.hashCode() : 0);
    result = 31 * result + (k3 != null ? k3.hashCode() : 0);
    result = 31 * result + (k4 ? 1 : 0);
    return result;
}
}
+2

hashCode()? , , .

+1

, , apache commons MultiKeyMap

+1

hashCode -

k1.hashCode() ^ k2.hashCode() ^ k3.hashCode() ^ k4.hashCode()

XOR , k4 hashCode , . k4 , k1, k2, k3 k4, - , HashMap.

+1

, - ( )? , () ? String-, , " " hocus pocus. .

+1

Source: https://habr.com/ru/post/1719216/


All Articles