aspose file tools
The moose likes Java in General and the fly likes Duplicate entries in HashSet Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Duplicate entries in HashSet" Watch "Duplicate entries in HashSet" New topic
Author

Duplicate entries in HashSet

MaheshS Kumbhar
Ranch Hand

Joined: Sep 24, 2009
Posts: 188

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?


I am slow but sure
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

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.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32662
    
    4
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
Marshal

Joined: Jul 08, 2003
Posts: 24054
    
  13

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.


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Duplicate entries in HashSet
 
Similar Threads
problems with hashset
HashSet ordering
TreeSet uses compareTo
adding objects with same hashcodes and true value return by equals() method to a set.
HashSet