About Question enthuware.ocpjp.v8.2.1137 :
Moderator: admin
-
- Posts: 125
- Joined: Thu Jul 05, 2018 6:44 pm
- Contact:
About Question enthuware.ocpjp.v8.2.1137 :
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....
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....
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1137 :
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.
You need to be able to spot this problem with the given hashcode and equals implementation for simple examples like the one given here.
If you like our products and services, please help us by posting your review here.
-
- Posts: 8
- Joined: Wed Oct 25, 2017 2:53 am
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1137 :
Hello. Why this implementation of hashCode is incorrect?
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1137 :
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 1
- Joined: Sun Jun 10, 2018 12:54 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1137 :
But the hashcode returns the same value! for both the objects how is it incorrect?
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1137 :
You really need to read the explanation carefully.
If you like our products and services, please help us by posting your review here.
-
- Posts: 1
- Joined: Fri Nov 22, 2019 2:15 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1137 :
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?
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?
-
- Posts: 33
- Joined: Sat Feb 15, 2020 12:44 am
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1137 :
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?
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?
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1137 :
Yes, but only partially.bvrulez wrote: What bothers me is that a HashMap should distinguish by hashcode, should it not?
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.How are two objects with the same hashcode handled in a HashMap? Or does HashMap create its own hash value for storing each key?
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 46 guests