.equals(o) and Set membership

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Murat Tasan

    .equals(o) and Set membership

    i have a class, in which i have override the Object.equals(O bject o)
    method... and then i try to make two equal objects (under the new
    definition) and place them into a HashSet... which should only accept one
    of them, as in the following code. the class holds two objects (a pair of
    objects) and if another member of the class contains the same two objects
    (in either place of the ordered pair), it is considered equal.

    Integer key1 = new Integer(1);
    Integer key2 = new Integer(2);
    HashSet x = new HashSet();
    // demonstrate that the equals() method works...
    System.out.prin tln((new MyClass(key1, key2)).equals(n ew myClass(key2,
    key1)));
    System.out.prin tln(x.add(new MyClass(key1, key2)));
    System.out.prin tln(x.add(new MyClass(key2, key1)));


    if i'm correct in reading the docs about how Set classes work, the output
    of this should be:

    true
    true
    false

    but instead i'm getting:

    true
    true
    true

    and indeed the Set x has two elements after this code.

    i thought the Set code checks for membership by applying the .equals()
    method, which at runtime should bind to my overridden method, no?

    thanks for any help,

    murat


    --
    Murat Tasan
    [email protected] u
    [email protected] .edu
    murat.tasan@cwr u.edu


  • Raymond DeCampo

    #2
    Re: .equals(o) and Set membership

    Murat Tasan wrote:[color=blue]
    >
    > i thought the Set code checks for membership by applying the .equals()
    > method, which at runtime should bind to my overridden method, no?
    >[/color]

    No, that is not correct. Whenever equals() is overloaded, hashCode()
    must be overloaded as well. The Collections classes depend on
    hashCode() and equals(). Read the javadoc for java.lang.Objec t
    carefully and implement hashCode() for your classes for which you have
    overridden equals() and you will see better results.

    HTH,
    Ray

    Comment

    • hiwa

      #3
      Re: .equals(o) and Set membership

      Murat Tasan <[email protected] u.edu> wrote in message news:<Pine.SOL. 4.53.0312041520 470.18529@homer >...[color=blue]
      > i have a class, in which i have override the Object.equals(O bject o)
      > method... and then i try to make two equal objects (under the new
      > definition) and place them into a HashSet... which should only accept one
      > of them, as in the following code. the class holds two objects (a pair of
      > objects) and if another member of the class contains the same two objects
      > (in either place of the ordered pair), it is considered equal.
      >
      > Integer key1 = new Integer(1);
      > Integer key2 = new Integer(2);
      > HashSet x = new HashSet();
      > // demonstrate that the equals() method works...
      > System.out.prin tln((new MyClass(key1, key2)).equals(n ew myClass(key2,
      > key1)));
      > System.out.prin tln(x.add(new MyClass(key1, key2)));
      > System.out.prin tln(x.add(new MyClass(key2, key1)));
      >
      >
      > if i'm correct in reading the docs about how Set classes work, the output
      > of this should be:
      >
      > true
      > true
      > false
      >
      > but instead i'm getting:
      >
      > true
      > true
      > true
      >
      > and indeed the Set x has two elements after this code.
      >
      > i thought the Set code checks for membership by applying the .equals()
      > method, which at runtime should bind to my overridden method, no?
      >
      > thanks for any help,
      >
      > murat[/color]

      My guess is: your equals() method is not equal to the Object#equals()
      method in its signature. I also made the error in my Java beginner
      days, numerous times may be!

      Comment

      • hiwa

        #4
        Re: .equals(o) and Set membership

        Murat Tasan <[email protected] u.edu> wrote in message news:<Pine.SOL. 4.53.0312041520 470.18529@homer >...[color=blue]
        > i have a class, in which i have override the Object.equals(O bject o)
        > method... and then i try to make two equal objects (under the new
        > definition) and place them into a HashSet... which should only accept one
        > of them, as in the following code. the class holds two objects (a pair of
        > objects) and if another member of the class contains the same two objects
        > (in either place of the ordered pair), it is considered equal.
        >
        > Integer key1 = new Integer(1);
        > Integer key2 = new Integer(2);
        > HashSet x = new HashSet();
        > // demonstrate that the equals() method works...
        > System.out.prin tln((new MyClass(key1, key2)).equals(n ew myClass(key2,
        > key1)));
        > System.out.prin tln(x.add(new MyClass(key1, key2)));
        > System.out.prin tln(x.add(new MyClass(key2, key1)));
        >
        >
        > if i'm correct in reading the docs about how Set classes work, the output
        > of this should be:
        >
        > true
        > true
        > false
        >
        > but instead i'm getting:
        >
        > true
        > true
        > true
        >
        > and indeed the Set x has two elements after this code.
        >
        > i thought the Set code checks for membership by applying the .equals()
        > method, which at runtime should bind to my overridden method, no?
        >
        > thanks for any help,
        >
        > murat[/color]

        Sorry, my previous reply is wrong. Java.util.HashX xx data structure
        classes check hashCode() of the added/stored objects. See javadoc for
        Object#hashCode () and source files HashSet.java and HashMap.java. The
        former depends on the latter in logic reuse.

        Comment

        • Murat Tasan

          #5
          Re: .equals(o) and Set membership

          thanks much, i totally forgot about that!

          murat

          On Fri, 5 Dec 2003, Raymond DeCampo wrote:
          [color=blue]
          > Murat Tasan wrote:[color=green]
          > >
          > > i thought the Set code checks for membership by applying the .equals()
          > > method, which at runtime should bind to my overridden method, no?
          > >[/color]
          >
          > No, that is not correct. Whenever equals() is overloaded, hashCode()
          > must be overloaded as well. The Collections classes depend on
          > hashCode() and equals(). Read the javadoc for java.lang.Objec t
          > carefully and implement hashCode() for your classes for which you have
          > overridden equals() and you will see better results.
          >
          > HTH,
          > Ray
          >
          >[/color]

          --
          Murat Tasan
          [email protected] u
          [email protected] .edu
          murat.tasan@cwr u.edu


          Comment

          Working...