File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes hashCode.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "hashCode.." Watch "hashCode.." New topic


andy armstrong
Ranch Hand

Joined: May 14, 2002
Posts: 154
What is best implementation of hashCode and
Why should we simply create a String and use the existing hashCode functionality in the from String class for most implementations of hashCode?
I see that in some instances for peformance intense apps this would not work well but for the majority of apps out there it seems the perfect solution.
String a;
String b;
StringBuffer sb = new StringBuffer();
return sb.toString().hashCode();


Certifications: <br />scp2, scea, scwcd, WL7.0, CISSP, XML
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

There's nothing terribly wrong with your idea, but it would have to be used with caution. A class Foo might implement toString() as

If you have a Foo as the only member in some other class, it might take you a long time to figure out why HashMaps performed so terribly when this latter class was used as the keys!

[Jess in Action][AskingGoodQuestions]
I agree. Here's the link:
subject: hashCode..
It's not a secret anymore!