• Post Reply Bookmark Topic Watch Topic
  • New Topic

Overriding hashCode()  RSS feed

 
Bill White
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Effective Java it is suggested that when dealing with arrays while overriding hashcode the best thing to do is to recurse through the array to get the appropriate value.
Does that line of thought hold for Vectors and other collection objects?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, generally. In fact the List, Set, and Map interfaces specify the required hashCode() algorithms very precisely (probably more precisely than necessary), and classes like AbstractSet and AbstractList implement it for you. (There's no AbstractMap; I guess this is implemented in each subclass.)
Note that this can get computationally intensive for large Collections or arrays. It may be a good idea to avoid using objects with internal collections as keys, if you can. Though some applications will still require to to use just this type of key. Just be aware that performance could be slower than you want.
[ April 21, 2003: Message edited by: Jim Yingst ]
 
Bill White
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!