aspose file tools*
The moose likes Beginning Java and the fly likes Doubt regarding working of equals() and hashCode() methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Doubt regarding working of equals() and hashCode() methods" Watch "Doubt regarding working of equals() and hashCode() methods" New topic
Author

Doubt regarding working of equals() and hashCode() methods

Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Hi all
Please have a look at the code below:




Please explain what is the flow of this code. I am unable to understand how and when the overridden hashCode() and equals() methods are being called.

Regards
Mansukhdeep


~ Mansukh
Peter Taucher
Ranch Hand

Joined: Nov 18, 2006
Posts: 174
HashMap#put checks following:
1) hash == hash
2) key == key || key.equals(key)
Both conditions must be met to determine if the key is already in the map.

First you put 't1' into the map. There're no other keys in the map, so it succeeds.
Then you put 't2' into the map. Because t1.equals(t2) delivers true and both keys have the same hashcode, the map says 'this key is already in the map' and overwrites it / replaces it's value to 'payBills'.
Then you put 't3' into the map. Because the equals method delivers false (the same hashcode isn't enough), the map says 'this key is not yet in the map' and inserts it.

Please correct me if I'm wrong.


Censorship is the younger of two shameful sisters, the older one bears the name inquisition.
-- Johann Nepomuk Nestroy
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Peter Taucher wrote:HashMap#put checks following:
1) hash == hash
2) key == key || key.equals(key)
Both conditions must be met to determine if the key is already in the map.

First you put 't1' into the map. There're no other keys in the map, so it succeeds.
Then you put 't2' into the map. Because t1.equals(t2) delivers true and both keys have the same hashcode, the map says 'this key is already in the map' and overwrites it / replaces it's value to 'payBills'.
Then you put 't3' into the map. Because the equals method delivers false (the same hashcode isn't enough), the map says 'this key is not yet in the map' and inserts it.

Please correct me if I'm wrong.



Thanks Peter :)
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Mansukhdeep Thind wrote:
Peter Taucher wrote:HashMap#put checks following:
1) hash == hash
2) key == key || key.equals(key)
Both conditions must be met to determine if the key is already in the map.

First you put 't1' into the map. There're no other keys in the map, so it succeeds.
Then you put 't2' into the map. Because t1.equals(t2) delivers true and both keys have the same hashcode, the map says 'this key is already in the map' and overwrites it / replaces it's value to 'payBills'.
Then you put 't3' into the map. Because the equals method delivers false (the same hashcode isn't enough), the map says 'this key is not yet in the map' and inserts it.

Please correct me if I'm wrong.



Thanks Peter :)


One more clarification. Does HashSet also check these two ??
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
Mansukhdeep Thind wrote: Does HashSet also check these two ??
Yes
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
That equals() method will not work; it must accept any kind of Object as its argument, including nulls. That will throw a NullPointerException if null is passes, and a ClassCastException if any object not from your class is passed.

Google for Effective Java Joshua Bloch and see whether you can find the "sample chapter" for the 1st edition (before 2008). That has a description of the equals() method in. You will find another description of the equals() method if you Google for Angelika Langer Java equals. Your hashCode() method, although strictly speaking correct, is very poor quality because it always returns the same value. You will find more about it in those two links, or if you find the 3rd edition of Bruce Eckel Thinking in Java (free on the net), you will find more about hash codes; Eckel acknowledges his method as being copied from Bloch.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
Another problem about keys in HashMap, or elements in HashSet: if you enter mutable objects, any change in their returned hash code may prevent you from finding the object again.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Campbell Ritchie wrote:Another problem about keys in HashMap, or elements in HashSet: if you enter mutable objects, any change in their returned hash code may prevent you from finding the object again.


Thanks sir. Will go through the book by Joshua and revert if I have any doubts about equals().
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
Mansukhdeep Thind wrote: . . . Thanks sir.
You're welcome
Will go through the book by Joshua and revert if I have any doubts about equals().
Page 33 in Bloch, second edition.

This post by Garrett Rowe contains another useful link.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Campbell Ritchie wrote:
Mansukhdeep Thind wrote: . . . Thanks sir.
You're welcome
Will go through the book by Joshua and revert if I have any doubts about equals().
Page 33 in Bloch, second edition.

This post by Garrett Rowe contains another useful link.


Aye Aye Sir
I have to ask this. You remember not only the names of the books and authors but the page numbers as well. Are you some kind of java GOD or someone?? I mean I would love to be an authority on what I do like you are. Just keep on guiding me.

Yours truly
Mansukhdeep Singh
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
Mansukhdeep Thind wrote: . . . page numbers as well. . . .
There is another way to do it, if you have a copy by your elbow. You open the book, find "Equals" in the index or otherwise, and the page has a 3 and a 3 on
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Campbell Ritchie wrote:
Mansukhdeep Thind wrote: . . . page numbers as well. . . .
There is another way to do it, if you have a copy by your elbow. You open the book, find "Equals" in the index or otherwise, and the page has a 3 and a 3 on


Haha!! Nicely said!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt regarding working of equals() and hashCode() methods