• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

hashCode() implementation

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I have a class which has three instance variables

say
boolean isAllowed
boolean isValid
String[] names

i have implemented the equals method by comparing all the three for the equality option

Now i have to implement the hashCode() for the class
String[] names - will always have atleast one name that is this array will have atleast one element in it.

so is it a reasonable choice to return the hashCode as names[0].hashCode()

or is there a better way to implement the hashCode() in the current case.

Any idea would be great.

Regards,
Vijay V
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's a reasonable way, but not an optimal way. An absolute requirement for hashCode is that two objects that are equal have the same hashCode, and your method does accomplish this. But a strong recommendation is that, to the extent possible, two objects that are unequal should have different hashCodes, and your version fails here; two objects with the same names[0], but one that's invalid and has 27 names, while the other is valid but has only one name, will be unequal but have the same hashCode. As a result, for example, they'll both be put in the same bin by a HashSet, leading to slower-than-optimal lookup times.

The best hashCode implementations, therefore, use all of the member data.
 
Ranch Hand
Posts: 884
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Allow me to recommend the Effective Java Programming Language Guide to you. There're 2 parts on overriding the equals() & hashCode() methods that I found them extremely useful.
 
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I recently ran across a site that has a very good description of how to properly implement hashCode() and equals() including sample code.
 
Vijay Venkat
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My thanks to Ernest Friedman-Hill for the crisp explanation and thanks to Cheng Wei Lee and Blake Minghelli for very good pointers.
 
Hey! You're stepping on my hand! Help me tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic