• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Behind HashSet

 
Naresh Shanmugam
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When we use hashset

1. Bucket number will be computed using "hashcode" method
2. Check if already an element with same value using "equals" method

But in the below program

Every time i add a new object i am getting a message saying "In Hashcode Method" (boz i have that message in my overriden hashcode method) but i am not getting the message "In Equals Method"(which i have added in my overriden equals method). Does that mean equals method is not invoked???
Hope equals method would be invoked everytime an object is added...
 
Henry Wong
author
Marshal
Pie
Posts: 21518
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Naresh Shanmugam wrote:
Hope equals method would be invoked everytime an object is added...


Why? The equals / hashcode contract says that if two objects are equal, then the hashcodes must be equal. So, if two hashcode are not equal, why do you need to bother with calling the equals() method?

Henry
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At the moment, this is what's happening:

- calculate the hashCode
- find the bucket
- the bucket is empty, so there's nothing to compare to

Try adding two HashsetChecks with the same value and see what happens.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50278
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please don't write "boz" or similar.
 
Naresh Shanmugam
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Henry and Matthew I got the point..

Sure Campbell i will follow that for sure..

 
Naresh Shanmugam
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now with the same code i showed above, i did this,



So for the first time the when "obj" is added

1. Hashcode method would have written a valid bucket number and since it is the new element "equals" method will not be invoked.

Now for the second time when the "obj" is added

1. Hashcode method will return the same bucket number, because this is the same object which we have added already, and now "equals method should have been called to check whether the same object is present",

But it is not called, any idea why it is not called Matthew and Henry??


Does that mean "hashcode" method does "equality check also???"
 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this -
 
Naresh Shanmugam
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


In the above scenario i am trying to add same "obj" twice, on the same bucket. Second time it should have called "equals" method to check for the presence of the same element(because already elements exist on the same bucket), but it didnt invoke "equals" method !!! --- Does that mean hashcode does some part of equality checking??
 
Campbell Ritchie
Sheriff
Pie
Posts: 50278
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Naresh Shanmugam wrote:Does that mean hashcode does some part of equality checking??
No.

You have to write your hashCode method to maintain the general contract for Object#equals(java.lang.Object) and Object#hashCode().
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Naresh Shanmugam wrote:In the above scenario i am trying to add same "obj" twice, on the same bucket. Second time it should have called "equals" method to check for the presence of the same element(because already elements exist on the same bucket), but it didnt invoke "equals" method !!! --- Does that mean hashcode does some part of equality checking??

Sure, hashCode() can do "some part" of equality checking - but not all of it. You are correct to think that a HashSet must do something else to confirm that two objects are really equal. Usually, that means calling equals(). But there is a third possibility - call == first. That's a shortcut to skip calling equals() in some cases. If two references are really == to each other, then equals() must also return true. (Assuming a valid implementation.) If you look at the source code for HashSet, you can probably find a place where they do something like this
 
Naresh Shanmugam
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much for valuable words Mike..
 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Naresh Shanmugam wrote:

In the above scenario i am trying to add same "obj" twice, on the same bucket. Second time it should have called "equals" method to check for the presence of the same element(because already elements exist on the same bucket), but it didnt invoke "equals" method !!! --- Does that mean hashcode does some part of equality checking??



It did not invoke...Well when i run it - I got this -

In Hashcode Method 2
In Hashcode Method 3
In Hashcode Method 4
Checking Equals Method while putting 4
 
Campbell Ritchie
Sheriff
Pie
Posts: 50278
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But if you have written your equals() method well, it will look like thisSo it should incorporate the == test anyway.

Two objects with equal hashcodes are not necessarily equal to each other. Two objects with different hashcodes must not be equal to each other.
 
Naresh Shanmugam
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I clearly understood the fundamentals now...
Thank you all for spending your valuable time..
 
Campbell Ritchie
Sheriff
Pie
Posts: 50278
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done, and you're welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic