Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

hashCode contract

 
Jomy George
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • 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?
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • 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: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Mohamed
is scjp6 asks questions about semantically correct hashcode()?
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • 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.
 
Malte Wannerskog
Ranch Hand
Posts: 92
Java PHP
  • Mark post as helpful
  • send pies
  • 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
 
Javin Paul
Ranch Hand
Posts: 295
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • 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
Java PHP
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic