| Author |
Using Exponential function for hashcode
|
satya ganesh
Greenhorn
Joined: Jul 14, 2008
Posts: 7
|
|
I understand that while overriding equals(Object) it is good to override hashcode() method in java. (This is used in hash based collections).
Here's my Question:
Is it necessary to use an exponential function to calculate hashcode(), String class's hashcode is calculated using an exponential function:
s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
Your valuable thoughts on this Please??
|
 |
Antany Vasanth
Ranch Hand
Joined: Jan 28, 2009
Posts: 43
|
|
Hi Satya,
Its not necessary to use an exponential function to calculate hashCode(). You can return any direct value or calculated value based on your requirement.
However make sure the hashCode() needs to return same value for the same objects.
Note* the hashCode() impacts the performance of hash based collections.
Regards,
Antany
|
 |
 |
|
|
subject: Using Exponential function for hashcode
|
|
|