aspose file tools
The moose likes Java in General and the fly likes Good implementation for the hashCode() for String objects Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Good implementation for the hashCode() for String objects" Watch "Good implementation for the hashCode() for String objects" New topic
Author

Good implementation for the hashCode() for String objects

Usha Pnatha
Greenhorn

Joined: Aug 01, 2008
Posts: 27
Hello Ranchers,

I'm required to implement a hashCode() for String objects. Each String will contain maximum 20 characters.

I went through the implementation in java.lang.String class. It's basically as follows:
s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]

Since there's no perfect hashing method, I believe that there can be alternatives to this way of implementation.

So, if anyone of you found a different implementation of the hashCode() for String objects(which would be more efficient than the provided one), please post you ideas here.

Thanks in advance,
Usha
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

It might be quicker to search the web for different hash code implementations--there are a lot.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Good implementation for the hashCode() for String objects
 
Similar Threads
Hashcode
HashCode
variables in hashCode( ) and equals( )
String objects
Strings