• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

hashCode..

 
andy armstrong
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();

}
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic