• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Mansukhdeep Thind
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ??
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mansukhdeep Thind wrote: Does HashSet also check these two ??

Yes
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!!
 
reply
    Bookmark Topic Watch Topic
  • New Topic