Sunday, May 15, 2011

Useful steps in implementing your own hashCode() method

Here are some useful guidelines for implementing the hashCode method correctly.

  1. Store an arbitrary non-zero constant integer value (say 7) in an int variable, called hash.
  2. Involve significant variables of your object in the calculation of the hash code, all the variables that are part of equals comparison should be considered for this. Compute an individual hash code int var_code for each variable var as follows -
    1. If the variable(var) is byte, char, short or int, then
      var_code = (int)var;

    2. If the variable(var) is long, then

      var_code = (int)(var ^ (var >>> 32));

    3. If the variable(var) is float, then

      var_code = Float.floatToIntBits(var);

    4. If the variable(var) is double, then -

      long bits = Double.doubleToLongBits(var);
      var_code = (int)(bits ^ (bits >>> 32));

    5. If the variable(var) is boolean, then

      var_code = var ? 1 : 0;

    6. If the variable(var) is an object reference, then check if it is null, if yes then var_code = 0; otherwise invoke the hashCode method recursively on this object reference to get the hash code. This can be simplified and given as -

      var_code = (null == var ? 0 : var.hashCode());

  3. Combine this individual variable hash code var_code in the original hash code hash as follows -

    hash = 31 * hash + var_code;

  4. Follow these steps for all the significant variables and in the end return the resulting integer hash.
  5. Lastly, review your hashCode method and check if it is returning equal hash codes for equal objects. Also, verify that the hash codes returned for the object are consistently the same for multiple invocations during the same execution.
The guidelines provided here for implementing equals and hashCode methods are merely useful as guidelines, these are not absolute laws or rules. Nevertheless, following them while implementing these two methods will certainly give you correct and consistent results.

No comments:

Post a Comment