Campbell Ritchie wrote:
Khushabu Wankhade wrote:i have inserted print statements in hashCode and equals method but its not reachable
Steve
I did not intend you to put print statements inside hashCode nor inside equals. If you had run the code I posted, you would have seen that the hash codes were different. And you would have seen, as Steve has explained, that you would look for those pairs in the wrong bucket in your Map.Khushabu Wankhade wrote:i have inserted print statements in hashCode and equals method but its not reachable . . .
Steve Luke wrote:Your problem is that you are re-using the same instance of dog. In the code in the original post, line 8 you create the dog instance. Then in line 15, you change the name stored in the dog instance to mangolian and in line 16 you try to get it. When you get it from the HashMap, the hashCode for dog is 9 (the length of the string "mangolian"), and there are no dogs in that bin in the HashMap so you get null as a result.
Then in line 17 you change the name stored in the dog instance to "nancy". Note that nancy has the same length as Rover, and so in line 18 when the HashMap goes to look up the dog, it finds an instance in the correct bin. This is the same instance dog that you created in line 8 and whose name you changed in line 17, so when HashMap calls its equals method it compares the value of the names - which will return true because you are using the same instance (the instance you put into the Map as a key to "Rover value" no longer has a name of Rover, it has a name of nancy.) That is why you get the value back in the second case.
Now in the third case, on line 19 you create a new dog instance and give it a name "Rover". This new instance will look into the correct bin, because it has the correct name length, but since the original instance doesn't have the name "Rover" any more (it has "nancy") the equals comparison will be false and you won't get a value returned.
Rule number one for an Object that will be used as a key in a map is to have good hashCode() and equals() implementations. Rule number two is to make it immutable - meaning the values it stores can't be changed. The fields must be private, assigned in the constructor, and only accessible via get() methods (not set() methods). Look up immutable objects for more info on how to do it right.
Campbell Ritchie wrote:
I did not intend you to put print statements inside hashCode nor inside equals. If you had run the code I posted, you would have seen that the hash codes were different. And you would have seen, as Steve has explained, that you would look for those pairs in the wrong bucket in your Map.Khushabu Wankhade wrote:i have inserted print statements in hashCode and equals method but its not reachable . . .
You are mistaken saying that the hashCode and equals methods are not reachable. Both are behaving as expected in this instance, and both are reachable. Their overriding behaviour adn polymorphism are correct.
The link I gave you earlier says that the behaviour of a Map is not specified. You cannot predict that you will find the “V” if you have mutable“K”s. You are changing the values used in the equals method, so you cannot expect the Map to behave predictably.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |