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 HashSet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "HashSet" Watch "HashSet" New topic
Author

HashSet

meeta gaur
Ranch Hand

Joined: Dec 05, 2012
Posts: 305


true
false

true
true

Set doesn't allow duplicate then why is it allowing ?


My one regret in life is that I am not someone else.
- Woody Allen
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3932
    
  17

meeta gaur wrote:
true
true

Set doesn't allow duplicate then why is it allowing ?

Because you are short-circuiting the definition of what a duplicate is. If the 'duplicates' don't have the same hashCode() then they most likely won't end up in the same bucket, and can't be considered equal. Since your code doesn't provide a good hashing function the two 'duplicate' objects will have different hashCode()s. Then, on the off chance they do get into the same bucket, you don't give a good equals() method. Since you don't tell the HashSet how to compare the objects, it relies on Object's implementation: and Object's implementation uses identity ( obj1 == obj2). Since your two objects are not the same exact Object this would be false.

By not providing your own equals() method you are not telling HashSet how to tell if two different objects are 'duplicates.' By not providing the a good hashCode() you are making sure they don't get close enough to even be compared as duplicates.


Steve
meeta gaur
Ranch Hand

Joined: Dec 05, 2012
Posts: 305


true
true

I was expecting true and false, because even if i do comment out hashCode(), it will use default hashCode() , so why doesn't equals work here ? why it can't check duplicate here ?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
It is not a default hashCode() but the implementation inherited from Object. That will probably give hash codes which differ even modulo 0x10, so the equals method is never called. You are not fulfilling the general contract for hashCode() that two objects which return true from equals() return the same hash code. You can therefore expect apparently incorrect behaviour from hash‑based collections.
Your implementation with * does fulfil the general contract for hashCode() and your object prints true false when executed.
meeta gaur
Ranch Hand

Joined: Dec 05, 2012
Posts: 305

Thanks.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7029
    
  16

meeta gaur wrote:I was expecting true and false, because even if i do comment out hashCode(), it will use default hashCode() , so why doesn't equals work here ? why it can't check duplicate here ?

And just to fortify what Campbell said: If you implement equals() and you want your object to work in a hashed Collection or Map, you MUST implement an equivalent hashCode() method; so it stands to reason that if you only comment out one of them, it won't work.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
meeta gaur
Ranch Hand

Joined: Dec 05, 2012
Posts: 305

Thank you Winston.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
You’re welcome
And remember: there is no such thing as a default hash code. Two distinct objects will almost certainly return different hash codes if you use the java.lang.Object#hashCode() implementation.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

@meeta gaur -
God is coming..

interesting. dont you feel their around you already !
meeta gaur
Ranch Hand

Joined: Dec 05, 2012
Posts: 305

Seetharaman Venkatasamy wrote:@meeta gaur -
God is coming..

interesting. dont you feel their around you already !

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HashSet
 
Similar Threads
equals and hashcode methods
hashCode() problem
Which principle(s) in Collection (HashSet) am I violating ?
hashcode and equals
Understanding equals() with hashcode()