Page 1 of 1
About Question enthuware.ocpjp.v8.2.1137 :
Posted: Tue Jul 31, 2018 10:15 pm
by __JJ__
This is a tricky question. I had thought, as long as a subset of fields used in equals is used in hc, and equal objects produce equal hashCodes, then at least superficially (ie ignoring things here like whether the fields might change during the pgm) it's all good.
I wasn't expecting having to work out a set of possible field values for which instances of the class might be calculated to be equal but which would return unequal hashCodes. But I guess it's here for a reason....
Re: About Question enthuware.ocpjp.v8.2.1137 :
Posted: Tue Jul 31, 2018 10:46 pm
by admin
The only thing that you need to see here is whether the given implemention of hashcode returns the same hashcode for two objects if the equals method for those two objects returns true.
You need to be able to spot this problem with the given hashcode and equals implementation for simple examples like the one given here.
Re: About Question enthuware.ocpjp.v8.2.1137 :
Posted: Wed Dec 05, 2018 2:35 am
by bortnikov_sa@mail.ru
Hello. Why this implementation of hashCode is incorrect?
Re: About Question enthuware.ocpjp.v8.2.1137 :
Posted: Wed Dec 05, 2018 2:49 am
by admin
Please go through the explanation carefully. It explains exactly why this hashCode is not correct with example. It does not satisfy the requirement of a valid hashCode method.
Re: About Question enthuware.ocpjp.v8.2.1137 :
Posted: Wed Mar 27, 2019 12:54 pm
by derlinio
But the hashcode returns the same value! for both the objects how is it incorrect?
Re: About Question enthuware.ocpjp.v8.2.1137 :
Posted: Wed Mar 27, 2019 7:03 pm
by admin
You really need to read the explanation carefully.
Re: About Question enthuware.ocpjp.v8.2.1137 :
Posted: Sun Nov 24, 2019 3:11 pm
by nerbinwalnoot
I had the correct answer, but i indeed did not understand it. In my question it says:
Info i1 = new Info("aaa", "aaa", "aaa"); Info i2 = new Info("aaa", "bbb", "ccc");
These will have the same s1 variable, so it is actually a correct implementation, right?
Re: About Question enthuware.ocpjp.v8.2.1137 :
Posted: Wed Feb 19, 2020 9:56 am
by bvrulez
It is indeed an invalide implementation of "hashcode" with respect to "equals", but in general according to oracle, "hashcode" needs not to be different for unequal objects.
https://stackoverflow.com/questions/164 ... e-hashcode
https://docs.oracle.com/javase/7/docs/a ... bject.html
What bothers me is that a HashMap should distinguish by hashcode, should it not? How are two objects with the same hashcode handled in a HashMap? Or does HashMap create its own hash value for storing each key?
Re: About Question enthuware.ocpjp.v8.2.1137 :
Posted: Wed Feb 19, 2020 12:06 pm
by admin
bvrulez wrote:
What bothers me is that a HashMap should distinguish by hashcode, should it not?
Yes, but only partially.
How are two objects with the same hashcode handled in a HashMap? Or does HashMap create its own hash value for storing each key?
You will need to read about it from a book or just from the JavaDoc description of HashMap because it is too much to write in post. Let us know if you don't understand anything.