aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Quiz Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Quiz" Watch "Quiz" New topic
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: 158
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: 158
Yes, as given by Santhosh above,
return a+b;
is also efficient.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Quiz
 
Similar Threads
hashCode() doubt
how to analyis which is correct hashcode?
hashcode()
equals and hashcode
Khalid Mock - Hashcode Q