Using a simple class with overridden .equals() for TreeSet.contains() doesn't work

Discussion in 'Java' started by StM, Dec 18, 2006.

  1. StM

    StM Guest

    I have this class, which is defined as an inner class of another class
    (so it's not the most beautiful class ever, but I needed it for a
    really simple task):

    #######################################################################

    class SimpleClass {
    String attrib_one;
    String attrib_two;
    String attrib_three;

    SimpleClass(String attrib_one, String attrib_two, String
    attrib_three) {
    this.attrib_one = attrib_one;
    this.attrib_two = attrib_two;
    attrib_three = attrib_three;
    }

    public boolean equals(Object obj) {
    if (obj instanceof SimpleClass) {
    SimpleClass sc = (SimpleClass) obj;
    return(attrib_two.equals(sc.attrib_two)
    && (attrib_one.equals(sc.attrib_one))
    && (attrib_three.equals(sc.attrib_three)));
    } else
    return false;
    }

    public int hashCode() {
    return attrib_one.hashCode() + attrib_two.hashCode() +
    attrib_three.hashCode();
    }

    public String toString() {
    return ("[attrib_one: " + attrib_one + "; attrib_two: " +
    attrib_two + "; attrib_three: " + attrib_three + "]");
    }
    }

    #######################################################################

    As you can see, there are three attributes and three methods of the
    Object class overwritten (equals(), hashCode() - i don't actually need
    it -, and toString()).

    Somewhere else in the code, i check if a SimpleClass object is already
    in a specified TreeSet

    #######################################################################

    TreeSet ts = new TreeSet();
    [...]
    SimpleClass sc = new SimpleClass(string1, string2, string3);
    if (ts.contains(sc)) {
    //blah blah
    }

    #######################################################################

    and in the "if" branch I put the generated object in the TreeSet
    itself.

    #######################################################################

    //blah blah
    ts.add(sc);
    //blah blah

    #######################################################################

    Now, the first .contains() is executed normally (I suppose that the
    TreeSet simply sees it's empty), while the second throws a
    ClassCastException. I don't know why is this happening. I'm absolutely
    sure that when i cast a (SimpleClass), the object IS a SimpleClass.

    I've searched the archives but I didn't find any clue. I'm using java
    1.4.2.

    Thanks for your help.

    StM
    StM, Dec 18, 2006
    #1
    1. Advertising

  2. StM

    Daniel Dyer Guest

    On Mon, 18 Dec 2006 14:06:37 -0000, StM <> wrote:
    > As you can see, there are three attributes and three methods of the
    > Object class overwritten (equals(), hashCode() - i don't actually need
    > it -, and toString()).
    >
    > Somewhere else in the code, i check if a SimpleClass object is already
    > in a specified TreeSet
    >
    > #######################################################################
    >
    > TreeSet ts = new TreeSet();
    > [...]
    > SimpleClass sc = new SimpleClass(string1, string2, string3);
    > if (ts.contains(sc)) {
    > //blah blah
    > }
    >
    > #######################################################################
    >
    > and in the "if" branch I put the generated object in the TreeSet
    > itself.
    >
    > #######################################################################
    >
    > //blah blah
    > ts.add(sc);
    > //blah blah
    >
    > #######################################################################
    >
    > Now, the first .contains() is executed normally (I suppose that the
    > TreeSet simply sees it's empty), while the second throws a
    > ClassCastException. I don't know why is this happening. I'm absolutely
    > sure that when i cast a (SimpleClass), the object IS a SimpleClass.
    >


    To use a TreeSet, your elements must implement Comparable or you must
    provide a Comparator to the constructor. You are doing neither. You
    haven't posted the actual error message, but presumably the TreeSet is
    trying to cast one of its elements to Comparable and failing.

    Dan.

    --
    Daniel Dyer
    http://www.uncommons.org
    Daniel Dyer, Dec 18, 2006
    #2
    1. Advertising

  3. StM

    StM Guest

    On 18 Dic, 15:17, "Daniel Dyer" <"You don't need it"> wrote:
    > To use a TreeSet, your elements must implement Comparable or you must
    > provide a Comparator to the constructor. You are doing neither. You
    > haven't posted the actual error message, but presumably the TreeSet is
    > trying to cast one of its elements to Comparable and failing.
    >
    > Dan.


    You're right; and I have to admit that I wouldn't have found this by
    myself: the error was misleading me, and besides I had already chosen
    an alternative solution; but I wanted to check anyway what I did wrong.

    Thank you very much.

    StM

    > --
    > Daniel Dyerhttp://www.uncommons.org
    StM, Dec 18, 2006
    #3
  4. Re: Using a simple class with overridden .equals() for TreeSet.contains()doesn't work

    StM wrote:
    > I have this class, which is defined as an inner class of another class
    > (so it's not the most beautiful class ever, but I needed it for a
    > really simple task):
    >
    > #######################################################################
    >
    > class SimpleClass {
    > String attrib_one;
    > String attrib_two;
    > String attrib_three;
    >
    > SimpleClass(String attrib_one, String attrib_two, String
    > attrib_three) {
    > this.attrib_one = attrib_one;
    > this.attrib_two = attrib_two;
    > attrib_three = attrib_three;


    Besides the lack of comparator or Comparable implemented, there's also
    the above error. It should say "this.attrib_three = attrib_three" or the
    attrib_three string of the class stays null.
    John Ersatznom, Dec 18, 2006
    #4
  5. StM

    StM Guest

    On 18 Dic, 21:45, John Ersatznom <> wrote:
    > StM wrote:
    > > I have this class, which is defined as an inner class of another class
    > > (so it's not the most beautiful class ever, but I needed it for a
    > > really simple task):

    >
    > > #######################################################################

    >
    > > class SimpleClass {
    > > String attrib_one;
    > > String attrib_two;
    > > String attrib_three;

    >
    > > SimpleClass(String attrib_one, String attrib_two, String
    > > attrib_three) {
    > > this.attrib_one = attrib_one;
    > > this.attrib_two = attrib_two;
    > > attrib_three = attrib_three;Besides the lack of comparator or Comparable implemented, there's also

    > the above error. It should say "this.attrib_three = attrib_three" or the
    > attrib_three string of the class stays null.


    I think this is an error of transcription (I, uh, anonymized the names
    of the variables). Thank you for pointing that out, anyway.
    StM, Dec 19, 2006
    #5
    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. Edward A Thompson
    Replies:
    4
    Views:
    527
    Tony Morris
    Feb 11, 2004
  2. GeorgeH
    Replies:
    3
    Views:
    475
    Patricia Shanahan
    Oct 14, 2006
  3. Joseph Turian
    Replies:
    5
    Views:
    585
  4. LastHope
    Replies:
    6
    Views:
    619
  5. Karsten Wutzke

    Contains/equals

    Karsten Wutzke, Aug 19, 2010, in forum: Python
    Replies:
    7
    Views:
    362
    Hrvoje Niksic
    Aug 24, 2010
Loading...

Share This Page