• 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

K&B chapter 7 question 15. Collections.

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[/code][/code]

Can some one tell me how to add objects to TreeSet?
Do I have to implement comparable in the Dog class to add the Dog object to be added to TreeSet? reason?
I am trying to do that in the above code but getting the compiler error please help..
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It doesn't compile because it is a primitive. You should wrap it.
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
use Integer size; in line 3 instead of int size; ,as the compareTo() method does not take primitives for comparison.

Comparable interface is implemented, for sorting. As TreeSet is a sorted Set, there is not need to implement Comparable for sorting.
You can add objects to TreeSet as,

as you have done in your code.
You dont have to implement comparable in the Dog class to add the Dog object to be added to TreeSet. Comparable is used for sorting, as in case of TreeSet as it is already Sorted in natural order, no need to implement Comparable for sorting. Comparable is implemented by List & Arrays for sorting. For adding to your TreeSet, just create a TreeSet as in line 1 and add objects to it, as in line2.

Hope this clears your doubt.
 
Dimple John
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



This does not work...It gives an Exception..
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, For what I understand in K & B

If you are adding OBJECTS in TreeSet you need the object to implement Comparable,

or you'll end up with a runtime exception.

If you are adding Wrapper Objects like String, Integer etc... They are sorted by natural order because wrappers implements comparable.

So if you are adding a Dog Object to your TreeSet the Dog should implement Comparable.

Hope this helps.
 
Anuradha Prasanna
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry, i was wrong. If we add the Dog objects to TreeSet, we have to implement Comparable Interface, otherwise there will be no means of comparing the Dog objects.
 
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How to implement compareTo() methods for primitive int type in this case to compare Dog by its size?
 
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dog class should implement comparable interface like this:


Try this to see if it works.
 
Janki Shah
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Helen Ma wrote:Dog class should implement comparable interface like this:


Try this to see if it works.


Yes, It worked.
Thank you Helen.
 
Thank you my well lotioned goddess! Here, have my favorite tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic