• 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 contract

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i learned that If equals() returns true then hashcode comparison should also return true.
but i wrote the program and it works fine



why this compile and run even if the contract violates?
or is the contract is just a specification which is not mandatory?
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Syntactically it is correct. But Semantically its wrong Try to put them in a Hashmap and try to retrieve the elements put in the map. This is where the contract comes into picture. It is because- it uses the hashCode to search for the relevant bucket and then uses equals() to find the exact element
 
Jomy George
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Mohamed
is scjp6 asks questions about semantically correct hashcode()?
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jomy George wrote:thanks Mohamed
is scjp6 asks questions about semantically correct hashcode()?



The question might be framed like this- Identify the correct implementation for equals() and hashCode() from the following options. Or they might mix it up with a collection based question. But its always important to remember the rules and also why those rules are there at the first place.
 
Ranch Hand
Posts: 92
PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mohamed Sanaulla wrote:Syntactically it is correct. But Semantically its wrong Try to put them in a Hashmap and try to retrieve the elements put in the map. This is where the contract comes into picture. It is because- it uses the hashCode to search for the relevant bucket and then uses equals() to find the exact element



Just as Mohamed Sanaulla mentions, your code will compile but it will not work properly since it doesnt follow the hashcode contract.
Its a big difference in code that compiles and code that works as intended.

Consider the following.



Even though obj1 and obj2 clearly are equal since obj1.equals(obj2) evaluates to true you cant find obj1 in the map using obj2 since the hashcodes are not equal.

As long as you clearly understand how equals and hashCode relate to eachother and are used in collections you should be fine.
Its described clearly in K&B book and im sure you can find lots of information about it if you search
 
Ranch Hand
Posts: 300
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

As Mohamed Sanaulla pointed out equals() and hashcode() methods usage while using HashMap or Hashtable. its worth noting to learn about correct implementation of equals() and hashcode() method , not only on exam point of view but also on work and interview point of view.

Based on my experience this is most sought of question asked during job interview and also very much important on SCJP exam as well.

worth noting "if two object are not equal as per there equals() method , they can still have same hashcode without violating contract."
 
Malte Wannerskog
Ranch Hand
Posts: 92
PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Javin Paul wrote:
worth noting "if two object are not equal as per there equals() method , they can still have same hashcode without violating contract."



Also worth noting is that it might not be very efficient if the hashCode() for object X always returns 1 for example. Perfectly valid but not very efficient, and thats the reason the hashCode exist, to make it more efficient.

Sorry for the slight OT.
 
reply
    Bookmark Topic Watch Topic
  • New Topic