This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

please explain the output?

 
Sonali Sehgal
Ranch Hand
Posts: 75
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is an enthuware question.

Consider the following class:




Which of the following statements are correct regarding the above class?
Option 1: This is an invalid implementation of hashCode() method with respect to the given equals() method.
Option 2: Only one of the Info objects will be stored in the HashMap.
Option 3:Both the objects will be stored in the HashMap however retrieving them using the Info objects as keys will not be possible.
Option 4:An exception will be thrown at run time at line //2.


Please explain what is the right answer for this question???

I am not able to understand this code of hashcode???
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Option 1 should be the correct answer. Please check the other post for explanation.
 
Balraj Momi
Ranch Hand
Posts: 45
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question 1 is correct is because hashCode() and equals() method should use same instance variables. And in this question, only one String s1 is being used in hashCode() method. This implementation will not fail, It will still work. But It is not in line with equals method.

Option 2: Wrong - Both object will be stored because both objects are not equal.
Option 3: Wrong - It will be possible to retrieve values from this map by using either of these objects as Key
Option 4: Wrong - No Exception thrown

Regards
Balraj
 
shivendra tripathi
Ranch Hand
Posts: 263
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The equal contract says, if equals() return for two object then hashCode() for these two object must be same. In the current implementation suppose I have two objects with filed values "aa" , "bb", "cc" and "bb", "aa", "cc". For this example equals() will return true but hashCode for these two objects won't be same.
I guess by now you must have got your ans. You can use a thumb rule that in proper hashCode implementation you should use all the information used in your equals() method implementation.
 
Jyoti B.Shah
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shivendra,

equals method will return false for two objects with filed values "aa" , "bb", "cc" and "bb", "aa", "cc" and hashcode for them will return different values, which is perfectly legal. I am not sure how the implementation of hashcode() method is invalid. It would be great if someone could provide a example where equals() will return true but hascode() for those two objects will return different values.

Output of following code returns -

false
hello
world

 
Jyoti B.Shah
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got answer to my question. If we have two info objects as {"aa","bb","cc"} and {"a","abb","cc"}, although they will be equal(s1+s2+s3 is same for both objects) but their hascode will be different which voilates the hascode() contract.
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Watch out with the example you just came up with.

{"aa","bb","cc"}, {"a","abb","cc"} and your implementation of equals means that two objects that are actually meaningfully different will pass the equals test. You should put some sort of separator into the check, like this..


or write different if statements that check for each variable on its own ...
 
shivendra tripathi
Ranch Hand
Posts: 263
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jyothi,

Good catch, I wanted to explain the scenario you mentioned "aa" , "bb", "cc" and "a", "abb", "cc" but somehow i put it in wrong way...
 
Sonali Sehgal
Ranch Hand
Posts: 75
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thanks to all of you for explaining me this topic of hashcode I think I am able to understand it better now.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic