# Quiz

jioy uilo
Greenhorn
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
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
Posts: 161
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.

jioy uilo
Greenhorn
Posts: 25
what abt a+b ?

La Vish
Ranch Hand
Posts: 161
Yes, as given by Santhosh above,
return a+b;
is also efficient.