File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes K&B chapter 7 question 15. Collections. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "K&B chapter 7 question 15. Collections." Watch "K&B chapter 7 question 15. Collections." New topic
Author

K&B chapter 7 question 15. Collections.

Dimple John
Greenhorn

Joined: Dec 08, 2009
Posts: 14
[/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

Joined: Oct 25, 2008
Posts: 2700

It doesn't compile because it is a primitive. You should wrap it.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Anuradha Prasanna
Ranch Hand

Joined: Mar 09, 2006
Posts: 115
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.


SCJP 6.0 90%
Dimple John
Greenhorn

Joined: Dec 08, 2009
Posts: 14



This does not work...It gives an Exception..
Neil Muya
Ranch Hand

Joined: Oct 26, 2009
Posts: 30
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

Joined: Mar 09, 2006
Posts: 115
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

Joined: Nov 23, 2011
Posts: 136
How to implement compareTo() methods for primitive int type in this case to compare Dog by its size?
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
Dog class should implement comparable interface like this:


Try this to see if it works.
Janki Shah
Ranch Hand

Joined: Nov 23, 2011
Posts: 136
Helen Ma wrote:Dog class should implement comparable interface like this:


Try this to see if it works.

Yes, It worked.
Thank you Helen.
 
GeeCON Prague 2014
 
subject: K&B chapter 7 question 15. Collections.