Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in hashCode()

 
Adil El mouden
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
This is a question found in the mock exam of mughal book:

Given the following class, which are correct implementations of the hashCode() method?

class ValuePair {
public int a, b;
public boolean equals(Object other) {
try {
ValuePair o = (ValuePair) other;
return (a == o.a && b == o.b)
|| (a == o.b && b == o.a);
} catch (ClassCastException cce) {
return false;
}
}
public int hashCode() {
// Provide implementation here.
}
}

Select the three correct answers.

a)return 0;

b)return a;

c)return a + b;

d)return a - b;

e)return a ^ b;

f)return (a << 16) | b;

answer are a, c, e

If answer a is true why then answer b it isnt?
Thank you.
 
Joe Sondow
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If two objects are equal according to the equals method, then they must have the same hashcode value in order to satisfy the equals-hashcode contract. Consider this situation:

You have a ValuePair object with a=2 and b=7, and you have another ValuePair object with a=7 and b=2. According to the equals method, these two objects are equal. Therefore, according to the contract, these two objects must return the same value from their hashCode() methods.

But if we choose return a; for the body of the hashCode() method, then one object will have a hashcode of 2, and the other will have a hashcode of 7, so we've broken the contract by writing an incorrect set of hashCode() and equals() implementations. If we chose return 0; then the two objects would have the same hashcode value, so that implementation would be correct.
 
Adil El mouden
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Joseph Sondow
 
Adil El mouden
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please can you explain why answer F is incorrect.
Thank you.
 
Philip Heller
author
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why isn't (F) an okay hash code?

Suppose a is 3 and b is 5. Then the hash code is (3 << 16) | 5. In hex this is 30005. Now consider a different instance, where a is 5 and be is 3. The hash code is (5 << 16) | 3, which in hex is 50003.

But the equals() method returns true when comparing these two objects, so the hash code contract is violated.

-- Phil
 
janki tangeda
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
F is incorrect because the expression in F evaluates to two different values when a=o.b & b=o.a.that means different hashcodes for object that are equal according to equals().thus it violates the equals() contract.
 
Adil El mouden
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many Thanks to Philip Heller.
I'm lucky!







And to janki tangeda too.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic