This week's giveaways are in the MongoDB and Jobs Discussion forums.
We're giving away four copies of Mongo DB Applied Patterns and 4 resume reviews from Five Year Itch and have the authors/reps on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Why is this HashSet allowing duplicates? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of Mongo DB Applied Patterns this week in the MongoDB forum
or a resume review from Five Year Itch in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Why is this HashSet allowing duplicates?" Watch "Why is this HashSet allowing duplicates?" New topic
Author

Why is this HashSet allowing duplicates?

Vidhya Ramaswamy
Ranch Hand

Joined: Oct 10, 2007
Posts: 65

Question taken from LearnKey MasterExam.
Why is the set allowing duplicates, and showing the size as 2?
I understand, it is because the equals() is not properly overridden, it doesn't take an Object. So, is the set allowing duplicate because of the equals() in Nearly class?
Ankit Garg
Saloon Keeper

Joined: Aug 03, 2008
Posts: 9189
    
    2

Yes! the equals method must take an Object parameter to make it a successful override and then it will not allow duplicates...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Vidhya Ramaswamy
Ranch Hand

Joined: Oct 10, 2007
Posts: 65
Thanks Ankit for your quick response.
Is this what happens?
calls equals() in the Nearly class.
equals() and hashCode() are called before adding to HashSet. But here, the equals() called is of Object class(since Nearly class does not override equals() properly. So, n2 is added to the set.
Please correct me if I'm wrong.
Ankit Garg
Saloon Keeper

Joined: Aug 03, 2008
Posts: 9189
    
    2

You are right. But if you change your code to something like this, then see the output. Change this

if(n1.equals(n2))s+="1";

to this

Object obj = n2;
if(n1.equals(obj))s+="1";

Now you will see that the objects are now not equal as the equals method in Object class will be called now instead of equals in Nearly class...
Preethi Dev
Ranch Hand

Joined: Sep 07, 2008
Posts: 265
Hi Ankit,
i tried this program ,as you have mentioned(i.e replacing Object b=n2)

but it still shows set.size() as 2. if it overrides equals() in object, then how it allows to add the second one?
please clear this too....

Thanks
Preetha
Vidhya Ramaswamy
Ranch Hand

Joined: Oct 10, 2007
Posts: 65
Hi Preetha,

Replacing the code with
Object obj = n2;
if(n1.equals(obj))s+="1";

calls the Object class equals(), which just does a direct compare(==).
So, 1 is not printed in this case.

The reason it shows set.size() as 2 is because both the references do not refer to the same object, as(n1!=n2).

Try this out(assigns n1 to obj, so both refer to the same object):

Object obj = n1;
if(n1.equals(obj))s+="1"; //calls Object class equals()
if(n1==n2)s+="2";
Set<Nearly> set = new HashSet<Nearly>();
set.add(n1);
set.add((Nearly)obj); //trying to add duplicate
System.out.println(s + " " + set.size());

Now, set.size() will print 1.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel/download
 
subject: Why is this HashSet allowing duplicates?
 
Similar Threads
equals and hashcode
doubt in hashcode...
mechanism of equals(Object)?
hashCode() and equals()