This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HashSet

 
meeta gaur
Ranch Hand
Posts: 305
Notepad Tomcat Server Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

true
false

true
true

Set doesn't allow duplicate then why is it allowing ?
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
meeta gaur
Ranch Hand
Posts: 305
Notepad Tomcat Server Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 48652
56
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 305
Notepad Tomcat Server Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.
 
Winston Gutkowski
Bartender
Pie
Posts: 10226
58
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
meeta gaur
Ranch Hand
Posts: 305
Notepad Tomcat Server Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Winston.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@meeta gaur -
God is coming..

interesting. dont you feel their around you already !
 
meeta gaur
Ranch Hand
Posts: 305
Notepad Tomcat Server Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seetharaman Venkatasamy wrote:@meeta gaur -
God is coming..

interesting. dont you feel their around you already !

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic