• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Hashcode

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where can I find good information on the hashcode contract?
Thank you
 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not really sure if there is a specified "contract" on what a hashcode method must do (other than return an int). The only thing to keep in mind is the following:

1. If two objects point to the same reference (i.e. a == b), then the hashcode for a and b MUST return the same value.

2. If the hashcode for a and b are equal, the objects MAY point to the same reference, but not necessarily.

If you are going to write your own custom hashcode method, make sure the above two items still hold true. Normally, the equals method is overriden if the hashcode method is, but not always (it depends on the implementation).
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The hashCode contract is defined in the API documentation for Object under the hashCode method.

Basically, if two objects are "equal" as defined by the equals method, then they must return the same hashCode. If they are not equal as defined by the equals method, then they are not required to return the same hashCode. Note that "equality" is defined here by the equals method, and not a simple reference comparison (==).
[ February 09, 2006: Message edited by: marc weber ]
 
A wop bop a lu bop a womp bam boom! Tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic