aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes hashCode contract Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "hashCode contract" Watch "hashCode contract" New topic
Author

hashCode contract

Jomy George
Ranch Hand

Joined: Jan 13, 2011
Posts: 62
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

Joined: Sep 08, 2007
Posts: 3056
    
  33

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


Mohamed Sanaulla | My Blog
Jomy George
Ranch Hand

Joined: Jan 13, 2011
Posts: 62
thanks Mohamed
is scjp6 asks questions about semantically correct hashcode()?
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3056
    
  33

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

Joined: Jan 06, 2011
Posts: 92

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

OCPJP (310-065) - 88%
Javin Paul
Ranch Hand

Joined: Oct 15, 2010
Posts: 281


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."


http://javarevisited.blogspot.com - java classpath - Java67 - java hashmap - java logging tips java interview questions Java Enum Tutorial
Malte Wannerskog
Ranch Hand

Joined: Jan 06, 2011
Posts: 92

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: hashCode contract
 
Similar Threads
Reg Hashcodes
Doubt in equals()
equlas()
equals() and hascode() doubt
Equals e HashCode