HashSet.addAll doesn't enforce uniqueness?

Discussion in 'Java' started by Paul Tomblin, Dec 8, 2006.

  1. Paul Tomblin

    Paul Tomblin Guest

    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
     
    Paul Tomblin, Dec 8, 2006
    #1
    1. Advertising

  2. Paul Tomblin

    Daniel Dyer Guest

    On Fri, 08 Dec 2006 19:24:15 -0000, Paul Tomblin
    <> 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
     
    Daniel Dyer, Dec 8, 2006
    #2
    1. Advertising

  3. 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
     
    Thomas Hawtin, Dec 8, 2006
    #3
  4. Paul Tomblin

    Paul Tomblin Guest

    In a previous article, (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
     
    Paul Tomblin, Dec 8, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. La'ie Techie
    Replies:
    0
    Views:
    425
    La'ie Techie
    Sep 26, 2003
  2. Anony!

    HashSet is strange

    Anony!, Aug 13, 2004, in forum: Java
    Replies:
    8
    Views:
    616
    Timo Kinnunen
    Aug 29, 2004
  3. Mike
    Replies:
    3
    Views:
    810
  4. Oliver Wong
    Replies:
    15
    Views:
    1,546
    Twisted
    Jul 21, 2007
  5. Adam Gardner
    Replies:
    5
    Views:
    163
    Sebastian Hungerecker
    Nov 19, 2008
Loading...

Share This Page