File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Behind HashSet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Behind HashSet" Watch "Behind HashSet" New topic
Author

Behind HashSet

Naresh Shanmugam
Ranch Hand

Joined: Jul 16, 2010
Posts: 84
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
Sheriff

Joined: Sep 28, 2004
Posts: 18917
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4425
    
    8

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

Joined: Oct 13, 2005
Posts: 39478
    
  28
Please don't write "boz" or similar.
Naresh Shanmugam
Ranch Hand

Joined: Jul 16, 2010
Posts: 84
Thank you Henry and Matthew I got the point..

Sure Campbell i will follow that for sure..

Naresh Shanmugam
Ranch Hand

Joined: Jul 16, 2010
Posts: 84
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???"
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

Try this -


Thanks and Regards,
SCJP 1.5 (90%), SCWCD 1.5 (85%), The Jovial Java, java.util.concurrent tutorial
Naresh Shanmugam
Ranch Hand

Joined: Jul 16, 2010
Posts: 84


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

Joined: Oct 13, 2005
Posts: 39478
    
  28
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

Joined: Mar 05, 2008
Posts: 3018
    
  10
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

Joined: Jul 16, 2010
Posts: 84
Thank you so much for valuable words Mike..
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

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

Joined: Oct 13, 2005
Posts: 39478
    
  28
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

Joined: Jul 16, 2010
Posts: 84
I clearly understood the fundamentals now...
Thank you all for spending your valuable time..
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
Well done, and you're welcome
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Behind HashSet