Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2006

Tip: Looking for answers? Try searching our database.

HashSet.addAll doesn't enforce uniqueness?

Thread view: 
Paul Tomblin - 08 Dec 2006 19:24 GMT
I'm finding the same object twice in my HashSet, and since I'm building
that HashSet using

Set getSelectedContentItems()
{
   Set      ciSet   = new HashSet();
   Iterator chIt = children.iterator();
   while (chIt.hasNext())
   {
     TreeNode child = (TreeNode)chIt.next();
     ciSet.addAll(child.getSelectedContentItems());
   }
   ciSet.add(myContentItem);
   return ciSet;
}

I'm wondering if somehow addAll is adding things without enforcing the
Set's constraint against duplicate items.  I've already checked that the
class "MyContentItem" class has a "public boolean equals(Object other)"
method.

Signature

Paul Tomblin <ptomblin@xcski.com> http://blog.xcski.com/
Networks are like sewers  ...  My job is to make sure your data goes
away when you flush, and to stop the rats climbing into your toilet
through the pipes.  -- Network administration, as told by Tanuki

Daniel Dyer - 08 Dec 2006 19:27 GMT
> I'm finding the same object twice in my HashSet, and since I'm building
> that HashSet using
[quoted text clipped - 16 lines]
> class "MyContentItem" class has a "public boolean equals(Object other)"
> method.

Does it also over-ride hashCode(), and are the two methods consistent with  
each other?

Dan.

Signature

Daniel Dyer
http://www.uncommons.org

Thomas Hawtin - 08 Dec 2006 19:32 GMT
> I'm finding the same object twice in my HashSet, and since I'm building
                                          ^^^^
> that HashSet using

> Set's constraint against duplicate items.  I've already checked that the
> class "MyContentItem" class has a "public boolean equals(Object other)"
> method.

And an @Override public int hashCode()?

Tom Hawtin
Paul Tomblin - 08 Dec 2006 19:48 GMT
In a previous article, ptomblin+netnews@xcski.com (Paul Tomblin) said:
>Set's constraint against duplicate items.  I've already checked that the
>class "MyContentItem" class has a "public boolean equals(Object other)"
>method.

Thomas and Daniel hit the nail on the head - I forgot to override
hashCode.  Thanks.

Signature

Paul Tomblin <ptomblin@xcski.com> http://blog.xcski.com/
C has features??  I thought the whole point of that language was to
offer nothing but bare metal.
             -- David P. Murphy



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.