• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

K&B chapter 7 question 15. Collections.

 
Dimple John
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • 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..
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't compile because it is a primitive. You should wrap it.
 
Anuradha Prasanna
Ranch Hand
Posts: 115
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



This does not work...It gives an Exception..
 
Neil Muya
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Janki Shah
Ranch Hand
Posts: 136
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to implement compareTo() methods for primitive int type in this case to compare Dog by its size?
 
Helen Ma
Ranch Hand
Posts: 451
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic