• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Doubt in hashCode() and equals() method

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Friends,

Given the code:



The output of above program is:
Hi Nitin the product of name.length()*age*salary for both the objects=90
and above formula is used for equals method
hi Nitin inside equals method ....returning TRUE....
Let's see what is returned by equals() method=true
Hi Nitin inside hashCode() method ...returning hashVal=45
Let's see what is the hashCode of object a=45
Hi Nitin inside hashCode() method ...returning hashVal=3
Let's see what is the hashCode of object b=3
So clearly hashCode is not same for above objects !!
Hi Nitin inside hashCode() method ...returning hashVal=45
Hi Nitin inside hashCode() method ...returning hashVal=3
Let's retrieve the objects now
Hi Nitin inside hashCode() method ...returning hashVal=45
hm.get(a)First Object
Hi Nitin inside hashCode() method ...returning hashVal=3
hm.get(b)Second Object
Hi Nitin inside hashCode() method ...returning hashVal=45
Hi Nitin inside hashCode() method ...returning hashVal=3
Hi Nitin inside hashCode() method ...returning hashVal=45
The Object retrieved from HashSet=com.dow.bean.NitEqualTest@2d
Hi Nitin inside hashCode() method ...returning hashVal=3
The Object retrieved from HashSet=com.dow.bean.NitEqualTest@3


Notice that equals() method returned true for both the objects but hashCode() method returned different values(45 and 3). This violates hashCode() contract. However there was no problem storing and retrieving both objects in HashMap and HashSet because they first invoked hashCode() method and based on that they stored each object in different bucket(separate memory area).
So what do you think that in above example the equals() method(uncommented one) implementation is correct or the equals() method(commented one) is correct?
(Note that I ran the above program as it is)
The above was a question in KS Mock Test that came with CD and while giving answer it was said that both implementations of equals() are correct with regards to hashCode() and equals() contract. Please correct me if I am wrong in saying this...

Thanks & Regards,
Nitin

[ August 17, 2007: Message edited by: Burkhard Hassel ]
[ August 17, 2007: Message edited by: Burkhard Hassel ]
 
Ranch Hand
Posts: 99
Mac Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think, in case of this sample the commented implementation should be a better one.
[ August 17, 2007: Message edited by: Al Mamun ]
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you're right:
The not-outcommented (upper) version breaks the contract, because the hashCode uses two variables, and the equals uses three.

Your version (outcommented and italics) would be appropriate.


Yours,
Bu.

-------
p.m.:
To indent your code properly:

At the posting page, mark the part of your posting that should be indented. Usually that will be your code (or some tabellaric output).

Hit the - Button below.
that's it!

(In your case I did this for you already)
 
Where all the women are strong, all the men are good looking and all the tiny ads are above average:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic