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*31^(n-1) + s*31^(n-2) + ... + s[n-1]
Your valuable thoughts on this Please??
Joined: Jan 28, 2009
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.