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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Devaka Cooray
• Knute Snortum
• Paul Clapham
• Tim Cooke
Sheriffs:
• Liutauras Vilda
• Jeanne Boyarsky
• Bear Bibeault
Saloon Keepers:
• Tim Moores
• Stephan van Hulst
• Ron McLeod
• Piet Souris
• Frits Walraven
Bartenders:
• Ganesh Patekar
• Tim Holloway
• salvin francis

# Doubt in hashCode()

Ranch Hand
Posts: 82
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.

Ranch Hand
Posts: 195
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.

Ranch Hand
Posts: 82
Thank you Joseph Sondow

Ranch Hand
Posts: 82
Please can you explain why answer F is incorrect.
Thank you.

author
Posts: 119
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

Ranch Hand
Posts: 54
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.

Ranch Hand
Posts: 82
Many Thanks to Philip Heller.
I'm lucky!

And to janki tangeda too.

 Mo-om! You're embarassing me! Can you just read a tiny ad like a normal person? ScroogeXHTML - small and flexible RTF to HTML converter library https://coderanch.com/t/710903/ScroogeXHTML-RTF-HTML-XHTML-converter