Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Hashcode and equals contract - K&B Mock

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

There is a mock question that says:

if(x1.hashcode()!=x2.hashcode()) - if the above statement evaluates to true, ie if hascodes for x1 and x2 are not equal, then x2.equals(x1) will always be true.

How?

It is appropriate according to the contract that2 objects can equal even if their hashcodes are not equal, but is it not a mandatory situation. Infact the above violates the equals contratc.....2 equal Objects should have equal hashcodes.

PLease help.


Please help.
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this may happen if we override the equals function of the Object class but not the hashcode function . so as a result the equals() may compare the two objects say with respect to there state where as the hashcode() will compare according to hashcodes . i am not very sure as overriding both the equals() and hashcode() if any one of them is overrided is mandatory or is suggested as better programming practice(contract) to keep the results of both the functions consistent as their origrnal implementation in the Object class .please do correct me if i am wrong
 
raghu dubey
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 Dipayan,

you are correct with what you say. It "may happen". It would even be legal, thought not appropriate/correct according to the hashcode/equals contract.

But the point is whether it should always happen. I dont think so. But I may be wrong.
 
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You'll have to provide us with a reference or link to the full mock question before we can say anything with certainty. Perhaps there's something else in the question that indicates or implies that the class in question does implement the hashCode() and equals() methods in a way that satisfies the contract.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy!

Raghu wrote:


It is appropriate according to the contract that 2 objects can equal even if their hashcodes are not equal, but is it not a mandatory situation.



The question:


if(x1.hashcode()!=x2.hashcode()) - if the above statement evaluates to true, ie if hascodes for x1 and x2 are not equal, then x2.equals(x1) will always be true.


The statement - as it is - is false.

The contract says:

If two objects are equal they must have the same hash code.

So if the hash codes are different, the two objects can not be equal. Provided they are fulfilling the contract.

Rember the bucket analogy: The hash codes are used for a sort of pre-grouping objects before the equals test is performed.
If the hash codes are different the equals test will not be performed.

If you have

Then the output is

true
[Raghu@ad3ba4, Raghu@1372a1a]


Equals is overridden to return true in any case, hashCode is not overridden, so it returns the hashCode of class Object which are always different.

Testing the equals method directly returns true due to the override, but when you really use the hashCode for hashing, both objects - despite the fact that they are equal - will be added to a set. Showing that the contract is violated.

I think the easiest way to understand this hashCode and equals contract is always to think of the bucket analogy.

Yours,
Bu.
 
The knights of nee want a shrubbery. And a tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic