• Post Reply Bookmark Topic Watch Topic
  • New Topic
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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

A question from Mock Test about hashCode

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please see the question below:

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 fulfill the equals() and hashCode() contracts for this class?
(Choose all the 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;




The correct answer is C and D.

Why D is correct?
I don't thnk that it can guarantee the contract between equal and hashCode.
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because option D isn't violate the equals or hashcode contract.
It's not optimal but not violating the contract

with

You will get a lot 2 Object that not equal but has same hashcode, although it's maybe not optimal but it's not violating the contracts
Because every equals object will generate same hashcode return.

So creating stricter (more variable involved) equals than hascode will not violating the contract. But if you created stricter hashcode than equals you violating the contract :
"If two objects are equal according to the equals(object) method, then calling the hashCode() method on each of the two objects must produce the same integer result."

I hope I explain it clearly
Pardon me if there're something wrong with my grammar or vocab
 
Steven Gao Song
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK.



The result is
true
false

I think that the answer from the Mock test is not correct.
Only C is the answer.
 
jimmy halim
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're right.
My mistake, I don't read the option well

It'll not violating the contract if the equals :


Using String's length and mathematic (*/+-) operation on equals is really not an equals
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ranchers,

the contract is clearly violated in answer D.
If two objects have different rate, they are equal but (may) have a diffent hashCode.

Where is the question from?


Yours,
Bu.
 
Steven Gao Song
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
It is from Mock Test of K&B.
 
Evildoers! Eat my justice! And this tiny ad's justice too!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic