The outcome is the same if == is replaced with .equals in the Dog's overriden equals() method
[My Blog]
All roads lead to JavaRanch
[My Blog]
All roads lead to JavaRanch
regards,
Arka
Hence the hashCode for abcd and mnop will be same allthough they are unequal. One principle is that if equals return true for two Objects then there hashCode() must also return similar values for the 2 objects. This principle is violated.
Another thing that I have found is that if you use equals() instead of "==" inside the equals() implementation will that make sense?
Christophe Verré wrote:The java.util.Map API states the following :
Note: great care must be exercised if mutable objects are used as map keys. The behavior of a map is not specified if the value of an object is changed in a manner that affects equals comparisons while the object is a key in the map.
Christophe Verré wrote:A special case of this prohibition is that it is not permissible for a map to contain itself as a key. While it is permissible for a map to contain itself as a value, extreme caution is advised: the equals and hashCode methods are no longer well defined on a such a map.
Christophe Verré wrote:You are using the same object, the one you have stored in the Map. So, if you change it's name, the object in the Map, which is the same, will also have its name changed. Names will always match.
All code in my posts, unless a source is explicitly mentioned, is my own.
SCJP 1.6, SCWCD 5.0, SCBCD 5.0 [loading..]
SCJP 1.6, SCWCD 5.0, SCBCD 5.0 [loading..]
SCJP 1.6, SCWCD 5.0, SCBCD 5.0 [loading..]
Himalay Majumdar wrote:
I dont think rehashing is part of the exam, just knowing hashcode searches for a bucket and equals searches for the object inside the bucket will do the trick.
Consider Paul's rocket mass heater. |