*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Why am I being able to add Duplicates to this set? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Why am I being able to add Duplicates to this set?" Watch "Why am I being able to add Duplicates to this set?" New topic
Author

Why am I being able to add Duplicates to this set?

Amit Batra
Ranch Hand

Joined: Mar 04, 2006
Posts: 361


How Am I being able to add these two duplicate objects to the set?. also can someone help me fix the iterator so i can go thru what I have added.
thanks
Vinayagar Karpagam
Ranch Hand

Joined: Apr 09, 2006
Posts: 72
I think SetExample has to override equals() method so that the contents are compared.

try adding this :

Amit Batra
Ranch Hand

Joined: Mar 04, 2006
Posts: 361
hey thanks man. Im only a few days away from exam and in my anxiety im forgetting my basics.

as for my second question to get the iterator to work toString() must be overriden.

Thanks both of you.
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
if instead you make:
SetExample s2=s1;
your output will be: 1

EDITED
but, even using your code with that equals(), output still is 2, because those two String Objects are in fact two diferent objects (diferent memory locations, i guess)
anyway i think Equals() is incomplete
[ August 13, 2006: Message edited by: miguel lisboa ]

java amateur
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
you also have to override hashCode:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why am I being able to add Duplicates to this set?