I know now a days inbuilt utilities available like HashCodeBuilder from Apache commons lang but i was trying to understand how to implement it myself and came across the example of hascode function for Employee class at http://en.wikipedia.org/wiki/Java_hashCode()
Everywhere on google, same kind of technique is suggested like multiplying non zero value with odd prime number and then summing it with instance variable(do it for instance variable).
Questions:-
1)why we can't return employeeId as hascode becoz it will aways be unique. Its simple and serves the hascode purpose. Agreed if it is not unique probably we need that kind of technique. Is that right?
2)Even If employee id is not unique, why its suggested to multiply with odd prime number? why taking any damn integer is not considered good?
Update:-
Peter i ran the example you mentioned it printed
[0, 32, 64, 96, 128, 160, 192, 224, 288, 256, 352, 320, 384]
[0, 32, 64, 96, 128, 160, 192, 224, 288, 256, 352, 320, 384]
i assume that output for now as yoy expected to understand the concept as you mentioned in your answer
[373, 343, 305, 275, 239, 205, 171, 137, 102, 68, 34, 0]
[0, 34, 68, 102, 137, 171, 205, 239, 275, 305, 343, 373]
As you suggested in your comment that this example demonstrated even unique hashcode can end up in same bucket. How this example demonstrated this behaviour? Do you mean 373 for integers and 0 for integers2 end up in same bucket ?
How prime number is helping in this example and how 34 would not have helped?