aspose file tools
The moose likes Java in General and the fly likes Using Exponential function for hashcode Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Using Exponential function for hashcode" Watch "Using Exponential function for hashcode" New topic
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

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using Exponential function for hashcode
 
Similar Threads
HashCode
hashcode() and equals()
String objects
String to int
String object ?