Win a copy of Rust Web Development this week in the Other Languages forum!
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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Tim Cooke
• Campbell Ritchie
• Ron McLeod
• Liutauras Vilda
• Jeanne Boyarsky
Sheriffs:
• Junilu Lacar
• Rob Spoor
• Paul Clapham
Saloon Keepers:
• Tim Holloway
• Tim Moores
• Jesse Silverman
• Stephan van Hulst
• Carey Brown
Bartenders:
• Al Hobbs
• Piet Souris
• Frits Walraven

# HashCode and Equals

Greenhorn
Posts: 29
• Number of slices to send:
Optional 'thank-you' note:
This Q is from K&B masterExam CD:

class SortOf{
String name;
int bal;
String code;
short rate;
public int hashCode(){
return(code.length()*bal);
}
public boolean equals(Object o){
// insert code here
}

Which of the following will fullfill the equals() and hashCode() contracts for
this class? (Choose all that apply)

a. return ((SortOf)o).bal == this.bal;
b. return ((SortOf)O).code.length() == this.code.length();
c. return ((SortOf)O).code.length() * ((SortOf)o).bal)) == this.code.length() * this.bal;
d. return ((SortOf)O).code.length() * ((SortOf)o).bal) * ((SortOf)o).rate))== this.code.length() * this.bal* this.rate;

Answer is c,d

Can someone please explain how d is correct?

if we have two instances of the class SortOf A and B with the following parameters

A
------
code.length() = 5
bal = 4
rate = 2

B
--------
code.length() = 5
bal = 2
rate = 4

using option D would make the two instances A and B equal i.e 5*4*2 = 5*2*4
Where as their hashcode would be different (5*4) != (5*2)

THIS BREAKS THE HASCODE, EQUALS CONTRACT that if two objects are equal, their hashcodes have to be equal.

In case a case how can D be correct?

Ranch Hand
Posts: 33
• Number of slices to send:
Optional 'thank-you' note:
Hi,
If we take 2 instances with values you have provided, they are not meaningfully equal, meaning the values of the instance fields of 2 instances (A and B) are not equal. The purpose of overriding hashCode() and equals() methods is to avoid duplicate values and fast searching. So even though equal method returns true and hashCode method doesn't, these are not considered as duplicate values in collection and always hashcode method is called before equals method..

Ranch Hand
Posts: 101
• Number of slices to send:
Optional 'thank-you' note:

So even though equal method returns true and hashCode method doesn't,

Hi Manju,
I suppose hashcode must return same value if equals return true.

Sheriff
Posts: 9691
42
• Number of slices to send:
Optional 'thank-you' note:
what Manju is trying to say is that in this example hashCode will return different values for two equal objects. This breaks the contract and this is why the code is behaving abnormally.

Ravi Yadav
Greenhorn
Posts: 29
• Number of slices to send:
Optional 'thank-you' note:
Manju/Ankit,

Doesnt D break the contract? I though the contract says if two objects are equal, their hashcodes should be equal too.

In this case the two objects A, B are equal because the equal methods return true. As per the contract, the hashcodes should be true as well...

what am I missing here...

Ravi Yadav
Greenhorn
Posts: 29
• Number of slices to send:
Optional 'thank-you' note:
I guess I missed the meaningfully equivalent part...
What does meaningfully equivalent means? who decides what's meaningfully equivalent? I thought the purpose of overridding equals() method was to take control on the factors that make two objects equal. In the case of A,B one might consider the two instances equal if the product of their fields are equal and not the individual fields. In such a case I cannot use the given hashcode right?.

Please advice....

Ranch Hand
Posts: 46
• Number of slices to send:
Optional 'thank-you' note:

Originally posted by Ravi Yadav:
I guess I missed the meaningfully equivalent part...
What does meaningfully equivalent means? who decides what's meaningfully equivalent? I thought the purpose of overridding equals() method was to take control on the factors that make two objects equal. In the case of A,B one might consider the two instances equal if the product of their fields are equal and not the individual fields. In such a case I cannot use the given hashcode right?.

Please advice....

Dear Ravi,

I think option (a) and option (b) are correct answers. Insted of C and D.

Regards,
Amardeep

reply