| Author |
Quiz
|
jioy uilo
Greenhorn
Joined: Mar 29, 2003
Posts: 25
|
|
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. } }
|
 |
Santhosh Manchala
Greenhorn
Joined: May 24, 2003
Posts: 2
|
|
Originally posted by jioy uilo: 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() { return a+b ; // Provide implementation here. } }
|
 |
La Vish
Ranch Hand
Joined: Apr 17, 2002
Posts: 154
|
|
The other possibilities,as I understand, are: return a|b; return a^b; return a&b; This is because even if the values in a and b are interchanged the return value will not change. By this logic return a-b; is not an efficient choice.
|
La Vish
SCJP 1.4, President 60s Club
|
 |
jioy uilo
Greenhorn
Joined: Mar 29, 2003
Posts: 25
|
|
|
what abt a+b ?
|
 |
La Vish
Ranch Hand
Joined: Apr 17, 2002
Posts: 154
|
|
Yes, as given by Santhosh above, return a+b; is also efficient.
|
 |
 |
|
|
subject: Quiz
|
|
|