• Post Reply Bookmark Topic Watch Topic
  • New Topic

equals method overrding  RSS feed

 
Amruta nikam
Greenhorn
Posts: 22
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


hi anybody please help me why my equals method is not reachable in this code....i override it...but i guess the objects equals method is getting called

i am getting output as=>

1 : Rover Value
Equals and hashCode test :
test1 : null
test2 : Rover Value
test3 : null



any body please help me i need output like given below why my equals method is not reachable


and i think it should be
1 : Rover Value
Equals and hashCode test :
test1 : null
test2 : null
test3 : Rover Value
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never use the == operator on reference types, except enum elements.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read this, especially the paragraph beginning ”Note: great care”.
 
Amruta nikam
Greenhorn
Posts: 22
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Read this, especially the paragraph beginning ”Note: great care”.


but i am comparing two objects using name attribute in equals method..still getting the same output
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Amruta nikam
Greenhorn
Posts: 22
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:




now I inserted Print statements in my equals and hashcode methods but its calling objects hashcode and equals method and not the overridden method...I m confused about the whole thing...whats happening over here
 
Amruta nikam
Greenhorn
Posts: 22
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have inserted print statements in hashCode and equals method but its not reachable
 
Amruta nikam
Greenhorn
Posts: 22
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Khushabu Wankhade wrote:i have inserted print statements in hashCode and equals method but its not reachable


my hahscode is reachable but not the equals method
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 56525
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Khushabu Wankhade wrote:i have inserted print statements in hashCode and equals method but its not reachable . . .
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.
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.
 
Amruta nikam
Greenhorn
Posts: 22
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


hey thanks...now i got it.....what I was thinking is even though I change the Dog name but the key is not going to change because i put it in the map before changing the Dog name...
that's where i went wrong ....So what i understood from your explanation is that...whenever i change the key value...its going to reflect in actual key inside the Map...means if I am putting "Clover" as a key and then changing the name to "Jimmy" then Map also have the key as "Jimmy" and not the "Clover" is that correct??
 
Amruta nikam
Greenhorn
Posts: 22
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Khushabu Wankhade wrote:i have inserted print statements in hashCode and equals method but its not reachable . . .
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.
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.

oh no no....you didn't...but I kept it intentionally to look that whether my equals and hashCode is reachable or not
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!