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?
-
1Related: http://www.javamex.com/tutorials/collections/hash_function_guidelines.shtml – Luiggi Mendoza Feb 25 '13 at 06:51
-
It very well could be - all depends on how hash function was defined – mvp Feb 25 '13 at 06:52
-
1Another resources: http://stackoverflow.com/q/2738886/1065197 http://stackoverflow.com/q/113511/1065197 ... you can find lot of this on the net. – Luiggi Mendoza Feb 25 '13 at 06:53
-
My answer would be to point out that new Integer(17) creates an object whose hashcode is 17. Anything that uses hashing in Java has to treat 17, or any other int, as a valid hashcode. – Patricia Shanahan Feb 25 '13 at 07:00
-
so, any possitive integer is valid hash code? or we should sum up the unicode for chars and calculate? – samuelebe Feb 25 '13 at 07:07
-
@samuelebe it would be *legal* but not a *valid* one. – Luiggi Mendoza Feb 25 '13 at 07:09
3 Answers
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
)

- 12,100
- 5
- 46
- 57
-
-
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
-
1Yes, 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
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).
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

- 1,323
- 1
- 15
- 18
-
what is the reason for down vote? can u give a valid reason as to why this answer is not helpful – tinker_fairy Feb 25 '13 at 07:07
-
I don't know who the downvoter was, but I'm sure he/she's a coward for not even leaving a comment. – Luiggi Mendoza Feb 25 '13 at 07:08