Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > HashSet.addAll doesn't enforce uniqueness?

Reply
Thread Tools

HashSet.addAll doesn't enforce uniqueness?

 
 
Paul Tomblin
Guest
Posts: n/a
 
      12-08-2006
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.

--
Paul Tomblin <> 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
 
Reply With Quote
 
 
 
 
Daniel Dyer
Guest
Posts: n/a
 
      12-08-2006
On Fri, 08 Dec 2006 19:24:15 -0000, Paul Tomblin
<ptomblin+> wrote:

> 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.


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

Dan.

--
Daniel Dyer
http://www.uncommons.org
 
Reply With Quote
 
 
 
 
Thomas Hawtin
Guest
Posts: n/a
 
      12-08-2006
Paul Tomblin wrote:
> 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
 
Reply With Quote
 
Paul Tomblin
Guest
Posts: n/a
 
      12-08-2006
In a previous article, ptomblin+ (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.


--
Paul Tomblin <> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to enforce registration ? Niclas ASP .Net 2 03-27-2006 06:06 PM
Can Thunderbird or Mozilla enforce same message rule Big Craigie Firefox 3 03-01-2005 12:43 AM
How to enforce "&"inside my queryString? Mehdi ASP .Net 2 01-29-2005 11:54 AM
Q: How to enforce singleton/get object handle between two separate processes? Jeff Java 3 07-31-2003 04:07 AM
How to enforce filename encoding? guava Java 1 07-11-2003 09:24 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57