• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

HashCode and Equals Contract Question from Inquisition Exam

 
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This question is from "Inquisition" simulator:

Given the following class House, which code fragments can be added to complete the equals() method without breaking the contract for hashCode()?


and the answers are :



As per the simulator correct answers are b,c and d.

I can understand b and c but d can give wrong result.

Please provide explanation.
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The contract says 'if two objects are equal, their hashCode must be the same'.

As for d, whichever combination of doors and windows you choose for which the equals method returns true, the hashCode will also be the same.
 
Harpreet Singh janda
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But taking a practical scenario, it is wrong. Right?
 
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
You would not calculate the hashCode the way it's done here.
 
Harpreet Singh janda
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am talking about the equals method implementation.

option d is

windows + doors == other.windows + other.doors

This is wrong because suppose one object is having 3 windows and 4 doors and other object is having 4 windows and 3 doors.
as per above statement if should be true, Which is wrong.
 
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
Yes, neither the equals nor the hashCode method would be implemented this way.
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Harpreet Singh janda wrote:I am talking about the equals method implementation.

option d is

windows + doors == other.windows + other.doors

This is wrong because suppose one object is having 3 windows and 4 doors and other object is having 4 windows and 3 doors.
as per above statement if should be true, Which is wrong.



This is not wrong. If we include this condition in the .equals() method:

windows + doors == other.windows + other.doors

then we are saying 2 House objects are the same if they have the same number of windows and doors e.g. a house with 2 windows and 5 doors is equal to a house with 1 window and 6 doors or a house that has 0 windows and 7 doors.

The hashcode contract states that two objects that are equal must return the same hashcode which they will in this instance.

The fact that you might not implement the hashcode() and equals method like this in the real world for House objects (you'd probably compare addresses or something) is not relevant to this question.

Therefore option d is 100% correct.
 
Ranch Hand
Posts: 49
1
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lets look at the contract for hash code and equals.

1. Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
2. If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
3. It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables.

b, c and d are correct

a. breaks rule 2. when windows=3 doors=3 and other.windows=3 doors=-3
b. works because each of the variables used in hashcode are equal
c. works for the same reason as b rule 3 allows the hash codes to be the same if equals() is not true
d. the sum of windows and doors are the same so objects that are equal have the same hash code.
e. breaks rule 2 two objects can be equal but with different hash codes.
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic