This week's giveaway is in the Spring forum.We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!See this thread for details.
Big Moose Saloon
 Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies Register / Login Win a copy of REST with Spring (video course) this week in the Spring forum!

# 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: 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.

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: 161
Yes, as given by Santhosh above,
return a+b;
is also efficient.

subject: Quiz