0

How to decide hashcode value? Recently I faced an interview question that "Is 17 a valid hashcode?". Is there any mechanism to define hashcode value? or we can give any number for hashcode value?

samuelebe
  • 149
  • 1
  • 3
  • 15

3 Answers3

3

Hashcodes should have good dispersion, so that different objects will be saved in different positions of the hash table (otherwise performance could be degraded).

From that point, while 17 us a "valid" hash code (in the sense that it is a 32-bit signed integer), it is suspicious how the hash function was defined.

For instance, a naive approach for hashing a string is just adding the value of each character. This result in similar hash values for simple strings (such as "tar" and "rat" that sum up to the same value).

A common trick is multiplying each value by a small prime, so that simple inputs will return different values, e.g.;

int result = 1;
result = 31 * result + a;
result = 31 * result + b;

or

int h=0;
for (int i = 0; i < len; i++) {
    h = 31*h + val[off++];
}

(the latter, from the JRE implementation of String.hashCode)

Javier
  • 12,100
  • 5
  • 46
  • 57
  • 17 would be a legal hashCode but not a valid one – Luiggi Mendoza Feb 25 '13 at 07:07
  • so, hash function can use any logic to generate a unique number, whether it be 17 or any positive integer number, right? – samuelebe Feb 25 '13 at 07:09
  • 1
    Yes, but normally you want it to remain constant during the lifetime of your object. If put an object in a HashMap and then "change" its hashCode (e.g. by modifying some object property), that HashMap might not be able to find the object anymore. – jackrabbit Feb 25 '13 at 07:11
  • Even `return 0`is a "valid" implementation of `hashCode` (albeit a terrible one). – Javier Feb 25 '13 at 07:12
  • @samuelebe is legal because you're returning an `int` value, but it's not valid because it doesn't fulfill the rules for a valid `equals` and `hashCode` implementation. This is explained in [`Object#hashCode`](http://docs.oracle.com/javase/6/docs/api/java/lang/Object.html#hashCode()) javadoc and the explanation with a sample [here](http://tutorials.jenkov.com/java-collections/hashcode-equals.html) – Luiggi Mendoza Feb 25 '13 at 07:15
  • @samuelebe also, for a valid `hashCode` implementation, the attributes used there should be immutable (they must not change) because calling the `hashCode` before and after changing the state of the object must give the same result. In this code sample, Javier doesn't explain if `a` and `b` attributes can change anytime leading to misunderstandings. – Luiggi Mendoza Feb 25 '13 at 07:18
0

Yes, 17 is a perfectly valid hashcode.

Whatever method you select to derive the hashcode, it should always return the same integer for the object (as long as its state remains the same).

0

Ya 17 is valid. Usually prime numbers like shown in the link are used, you can implement hashcode using the id of that entity which is a primary key

public int hashCode()
{
    int result = 17;
    result = 37 * result + (getId() == null ? 0 : this.getId().hashCode());
    return result;
}

this provides different ways for implementing the hascode

tinker_fairy
  • 1,323
  • 1
  • 15
  • 18