• Post Reply Bookmark Topic Watch Topic
  • New Topic

Duplicate entries in HashSet  RSS feed

 
MaheshS Kumbhar
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

In above code sample we have not overridden hashCode method, so default Object.hashCode method will be used which allows duplicate entries to be added in HashSet. But duplicate entries are not added.
Moral : I have heard that if we dont override hashCode method you will end up with Set with duplicate entries , is it correct?
 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From another thread:
Jesper Young wrote:Please use the code tags correctly; don't double-click the Code button, because it will open and immediately close the code block.

You also shouldn't (incorrectly) use quote tags instead of code tags.

MaheshS Kumbhar wrote:In above code sample we have not overridden hashCode method, so default Object.hashCode method will be used which allows duplicate entries to be added in HashSet. But duplicate entries are not added.

Your class itself has not overridden hashCode, but you are not adding instances of that class but of String, and the String class does override hashCode.
Moral : I have heard that if we dont override hashCode method you will end up with Set with duplicate entries , is it correct?

There is a slight, slight, slight chance that some duplicates will be regarded as one*, but you can ignore that chance. So yes, essentially, you are correct.


The hashCode return value is modified somewhat by the HashSet (and HashMap and Hashtable) to map to a "bucket". It is possible that two different hash codes map to the same bucket, but like I said, the chance is very small.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It might be more precise to say that two apparently identical objects can be inserted into the same set. If you don't override hashCode and equals, then the set will regard the two objects as completely different.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:
The hashCode return value is modified somewhat by the HashSet (and HashMap and Hashtable) to map to a "bucket". It is possible that two different hash codes map to the same bucket, but like I said, the chance is very small.


Actually, the chance is not that small, but it's not a problem at all. It will happen whenever two objects have the same hashCode (which is relatively rare, for a well-designed hashCode method) or their hashCode mapping onto the relatively small number of buckets is the same (this is far more common.) But a hash bucket can hold multiple objects by design. Objects within a single bucket are distinguished using their equals() method. If two objects that hash to the same bucket report that they're equals(), then they're considered the same, and the second and subsequent objects will be discarded.

This is the relationship between hashCode() and equals() that makes it important to override hashCode when you override equals(). If two objects hash to different buckets, their equals() methods will never be used to compare them for equality.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!