aspose file tools*
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


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "hashCode.." Watch "hashCode.." New topic
Author

hashCode..

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.
e.g.
...
String a;
String b;
hashCode(){
StringBuffer sb = new StringBuffer();
sb.append(a);
sb.append("|");
sb.append(b);
return sb.toString().hashCode();

}


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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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: http://aspose.com/file-tools
 
subject: hashCode..