Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How hashcodes for enums are calculated in Java, and combining enum hashCodes for a HashMap's key

I have a class that contains different enums (different types). This class is used as key for a HashMap. The classes hashCode currently is implemented like this:

  public static class Key implements Comparable<Key> {
    final int a;
    final Enum1 enum1;
    final Enum2 enum2;

    @Override
    public int hashCode() {
      return a ^ enum1.hashCode() ^ enum2.hashCode();
    }

    // ... definition of equals and toString ...
  }

Now if enums hashCode would just return the index of the enum value in the enum's definition, this would not be optimal (too many clashes). The method definition for Enum.hashCode() is this:

/**
 * Returns a hash code for this enum constant.
 *
 * @return a hash code for this enum constant.
 */
public final int hashCode() {
    return super.hashCode();
}

Assuming this delegates to Object.hashCode(), everything should be fine because for every enum constant there only exists one instance, and Object.hashCode() will in theory be something like an integer derived from the internal address of the object. Am I right?

PS: Of course you will have to use something more complex when the same enum is used several times in a key.

like image 287
Axel Avatar asked Nov 27 '12 09:11

Axel


People also ask

What is the Hashcode of an enum?

As it is said above,Enum are immutable in the Java, So the hashcode generated for a Enum is a perfect key for a Hash collection,just as the String are perfect keys. The enum declaration is a special kind of class declaration. An enum type has public, self-typed members for each of the named enum constants.

Can you use == for enums?

Because there is only one instance of each enum constant, it is permissible to use the == operator in place of the equals method when comparing two object references if it is known that at least one of them refers to an enum constant.

How are enums implemented in Java?

Enums are statically created when the enum class is first loaded and are immutable. You must have a constructor in the enum class if you want to assign different values to your enum. After the constructor was finished you cannot change the enums value (immutable as said).


2 Answers

Yes, you are right in that the hashcode of an enum element will come from the static instance, bound to memory positions, and be unique.

On the other hand, there are better ways of generating a hashcode with less collision probability. Check out, for example, the defaults that eclipse can autogenerate for you (right click, Source> Generate hashCode and equals)

public int hashCode() {     final int prime = 31;     int result = 1;     result = prime * result + ((enum1 == null) ? 0 : enum1.hashCode());     result = prime * result + ((enum2 == null) ? 0 : enum2.hashCode());     return result; } 

By throwing prime numbers into the mix (the precise math escapes me) you are supposed to be a little more resistant.

Note you can also let eclipse generate an equals method for you! (Even a toString). Not saying you must blindly trust them, but they are usually a very good start.

like image 137
Miquel Avatar answered Sep 23 '22 17:09

Miquel


As it is said above,Enum are immutable in the Java, So the hashcode generated for a Enum is a perfect key for a Hash collection,just as the String are perfect keys.

The enum declaration is a special kind of class declaration. An enum type has public, self-typed members for each of the named enum constants. All enum classes have high-quality toString, hashCode, and equals methods. All are Serializable, Comparable and effectively final. None are Cloneable. All of the "Object methods" except toString are final: we take care of comparison and serialization, and ensure t hat it's done right.

like image 40
Marian-Daniel Craciunescu Avatar answered Sep 26 '22 17:09

Marian-Daniel Craciunescu