• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

TreeSet question

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
I have the following code. Can anyone please tell me why the second println prints true while the first println prints false.
I looked up the API of Set, and it says

Returns true if this set contains the specified element. More formally, returns true if and only if this set contains an element e such that (o==null ? e==null : o.equals(e)).




Thanks in advance.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is this Node class you're using? My guess is that it doesn't override equals() or compareTo() correctly, and falsely reports that the two instances are equal (or compareTo() == 0).
 
Brian Lin
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just did a little more testing on TreeSet. Here is what I got.

The output is:
Are they equal: false
Do they have the same hashcode: false
0
Is a equals to b: false
Is b in the TreeSet: true
1
How could this be happening? a and b have are not equal, and they have different hashCode. why is b in the TreeSet after I added a only? It seems to me that the contains() method only looks at compareTo method, and return true if compareTo returns 0.
in the above if I change the compareTo method and return -1 (it is bad, used just for demonstration purpose). then s.contains(b) in the last line of main method will return false.
Any idea?
Thanks in advance.
[ edited to break long line and to remove the evil tab character -ds ]
[ April 27, 2004: Message edited by: Dirk Schreckmann ]
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about writing a proper compareTo method?
If you're unsure about the proper operation of the compareTo method, perhaps you'd taking a look at the Comparable interface documentation, which describes how the compareTo method is supposed to work.
[ April 27, 2004: Message edited by: Dirk Schreckmann ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic