The outcome is the same if == is replaced with .equals in the Dog's overriden equals() method
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.
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.
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koophttps://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton