Re: .equals(o) and Set membership

From: hiwa (HGA03630_at_nifty.ne.jp)
Date: 12/05/03

  • Next message: hiwa: "Re: .equals(o) and Set membership"
    Date: 4 Dec 2003 21:38:45 -0800
    
    

    Murat Tasan <tasan@eecs.cwru.edu> wrote in message news:<Pine.SOL.4.53.0312041520470.18529@homer>...
    > i have a class, in which i have override the Object.equals(Object 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.println((new MyClass(key1, key2)).equals(new myClass(key2,
    > key1)));
    > System.out.println(x.add(new MyClass(key1, key2)));
    > System.out.println(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

    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!


  • Next message: hiwa: "Re: .equals(o) and Set membership"

    Relevant Pages

    • RE: Equality vs Sameness
      ... Equals would not offer any benefit. ... >> from it in the future if the users haven't asked for that functionality. ... So you override Equals and GetHashCode even for forms, ... or wanted to compare two instances of a singleton? ...
      (microsoft.public.dotnet.languages.csharp)
    • Re: comparing objects
      ... I override the Equal method on SalesRegion ... As Arne already put it, the Contains method is using the Equals method, ... ArrayList salesRegions = ... private string s; ...
      (microsoft.public.dotnet.languages.csharp)
    • RE: Find object inside arraylists by object attribute
      ... you will need to override the Equals method inside your class such that it ... public override int GetHashCode() ... instead that you just use that in your arraylit instead: ...
      (microsoft.public.dotnet.languages.csharp)
    • Re: Find object inside arraylists by object attribute
      ... > public override bool Equals ... > //whenever you override equals you should override GetHashCode too ... > public override int GetHashCode() ... > instead that you just use that in your arraylit instead: ...
      (microsoft.public.dotnet.languages.csharp)
    • Re: override GetHashCode
      ... public override int GetHashCode() ... if two instances of SomeType are equal (cause _field equals), ... > public override int GetHashCode() ... >> the Hashtable uses the hash code of the key to determine the bucket that>> stores the key. ...
      (microsoft.public.dotnet.languages.csharp)