• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

please explain the output?

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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???
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Option 1 should be the correct answer. Please check the other post for explanation.
 
Ranch Hand
Posts: 45
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Crusading Chameleon likes the size of this ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic