• 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

hashCode()/equals()

 
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have been stuck with this topic since a long time and still not clear with the use/implementaion of equals() and hashCode()

In the following code, how does commenting or uncommenting hashCode()
effect the out put.

Commenting - output-> 3
Uncommenting - output-> 2

Also, which two objects are being compared in equals() when put() is called.

I know when put() is called hashCode() is implicitly called to store in the right "bucket" and equals() is called to make sure there is no duplicate objects.
But which two objects are being compared - like in the case of
m.put(t1,"Do laundary");
( Ther is only one object here)

I basically did not get the implementation of hashCode() and equals() when its illegal legal or efficeint and how to differentiat between them.

If some one has a better way of understanding this topic,diagramitcallly or simple programs please Help Me!

This code is from K&B.

 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you don't override the hashCode method, then your class will inherit the implementation from Object.

In this example, your hashCode implementation returns a constant (8). So all instances -- equal or not -- will return the same hashCode, which is okay.

But if you comment this out and use Object's implementation of hashCode, then equal instances will not return the same hashCode (unless you happen to be comparing an instance to itself). This is not okay, because equal instances will end up in different hashCode buckets, and your results will not be reliable. (In particular, this allows your code to put multiple elements in the HashMap that have duplicate keys, which is why you have 3 elements rather than 2.)
[ May 12, 2008: Message edited by: marc weber ]
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Nabila Mohammad:
...I know when put() is called hashCode() is implicitly called to store in the right "bucket" and equals() is called to make sure there is no duplicate objects.
But which two objects are being compared - like in the case of
m.put(t1,"Do laundary");
( Ther is only one object here)...


Map keys must be unique. So when you put an element in a HashMap, this object's key is being compared to the other keys already in the Map.

When you remove the hashCode implementation (and default to the implementation inherited from Object), it's not able to find the duplicate key ("Monday") because it's looking in the wrong hashCode bucket. This is why it allows the duplicate entry.
 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Also, which two objects are being compared in equals() when put() is called.

I know when put() is called hashCode() is implicitly called to store in the right "bucket" and equals() is called to make sure there is no duplicate objects.
But which two objects are being compared - like in the case of
m.put(t1,"Do laundary");
( Ther is only one object here)



Hope this below code helps

When one object is added to the Map, equals method is not called. Only hashCode() is called and it returns 8 and the key-value pair is put in the hash bucket with number 8.


When another key-value pair is added, first hashCode() is called and returns the same value 8 and then the equals() is called and in the equals method the new key is compared with the older key if it is a duplicate then the older key is overwritten with the new key-value pair. Hence the size will still be 1.



prints:

Comparing: Monday Monday
Comparing: Wednesday Monday
2

Somebody please correct me if im blurting some nonsense. Thanks
[ May 12, 2008: Message edited by: sridhar row ]
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Sridhar.

That was a brilliant example!
Cleared a lot of my doubts regarding the comparision that takes place.

Marc, Thanks - I could connect and link the stuff which I had in my mind which I earlier couldn't relate to..


If I consider a Map as a large table containing Buckets -


In the first case where I implement the hashCode() returning 8,
It would mean ther is one bucket and all the objects are being placed in that bucket.
However since Map cannot have duplicate elemens ,it returns the size 2 as it doesnot detect the duplicate element.

In the second case ther are three buckets each having an object - t1,t2,t3.
Irrespective of whether they are equal or not.
So it returns the size 3, even if the implementation is not correct...


Am I thinking on the right track...?!?!
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Nabila Mohammad:
...Am I thinking on the right track...?!?!


Yes.

When you "put" an object in a HashMap, it calculates a hashCode for the new object's key, then looks in that hashCode bucket to see if the same key already exists in the map.

If hashCode returns a constant, then everything will be in the same hashCode bucket. It's not an efficient implementation for the map, but it will work, because everything will be found where expected.

On the other hand, if hashCode is implemented incorrectly and returns different values for "equal" keys, then keys will not be found in the expected buckets. This will cause problems like duplicate keys being added, and not being able to retrieve values based on keys.
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it..(I think)
Thanks Alot!!
 
sridhar row
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

That was a brilliant example! [thumb]
Cleared a lot of my doubts regarding the comparision that takes place.



Thanks!! I'm glad it helped.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic