• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Question on Collections & Generics for SCJP 5.0

 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


With the code above, I have the following questions :-

The above code prints (as expected):-

Name = FRUIT, Sweetness = 0
Name = APPLE, Sweetness = 1
Name = GALAAPPLE, Sweetness = 3
Name = MACAPPLE, Sweetness = 5

1. Now when I comment out the entire equals(), I still get the same output as above, i.e.,

Name = FRUIT, Sweetness = 0
Name = APPLE, Sweetness = 1
Name = GALAAPPLE, Sweetness = 3
Name = MACAPPLE, Sweetness = 5

If you note above, I am adding two new apple objects to TreeSet. Since I dont have my own equals(), the equals() of the parent Object class will be used which only checks for == operator, but I added two new apple objects, so why doesnot my collection have 5 objects as I added?

2. Now If I comment out the contents of the compareTo() and let it return 0 all the time, the output now becomes :-

Name = MACAPPLE, Sweetness = 5

I understand this since when you add the second element, the compareTo() will return 0 which means the object is already present and so it wont be added.

However if I let it return -1, then the output becomes :-

Name = APPLE, Sweetness = 1
Name = GALAAPPLE, Sweetness = 3
Name = FRUIT, Sweetness = 0
Name = APPLE, Sweetness = 1
Name = MACAPPLE, Sweetness = 5

And when it returns +1, when the output becomes :-

Name = MACAPPLE, Sweetness = 5
Name = APPLE, Sweetness = 1
Name = FRUIT, Sweetness = 0
Name = GALAAPPLE, Sweetness = 3
Name = APPLE, Sweetness = 1

How do we explain this behavior ? What order is being used here ?

Also notice that my collection now show 5 elements. If I revert the compareTo() to its original good logic, then the size drops to 4 elements..

pls enlighten me on whats going on here....
[ July 13, 2005: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please, as a courtesy to your readers, use tags to suround formatted code.

And please use class names with capital first letters (that is, follow the normal Java conventions). For example Apple, MacApple, GalaApple.
[ July 13, 2005: Message edited by: Barry Gaunt ]
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Rupak:
I saw you implemented java.lang.Comparable which only has one method compareTo(T o) and doesn't have equal method.
I thought you may want to implement java.util.Comparator which has methods compare(T o1, T o2) and equals(Object obj).

I don't know if this the reason you can't get expected result since I don't have JAVA 5.0 to run.
 
Rupak Khurana
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
TreeSets & TreeMap rely on their elements to implement java.lang.Comparable interface.
 
reply
    Bookmark Topic Watch Topic
  • New Topic