This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills and have John Sonmez on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Plz xplain this code on TreeSet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Plz xplain this code on TreeSet" Watch "Plz xplain this code on TreeSet" New topic
Author

Plz xplain this code on TreeSet

Niranjan Deshpande
Ranch Hand

Joined: Oct 16, 2005
Posts: 1277
Can anyone please tell me in simple laguage what�s going on ??? Please give me a verbose explanation.


/* whats the output ??
1.Compilation succeeds, compare() throws ClassCastException
2.Compilation succeeds, the output is 0 1 2 3
3.Compilation succeeds, the output is 1 2 3
4.Compile time error, the compare() method does not have the
correct signature
5.Compile Time error, compareTo method from the Comparator
interface has not been implemented.
[ November 02, 2005: Message edited by: Mark Spritzler ]

SCJP 1.4 - 95% [ My Story ] - SCWCD 1.4 - 91% [ My Story ]
Performance is a compulsion, not a option, if my existence is to be justified.
Seb Mathe
Ranch Hand

Joined: Sep 28, 2005
Posts: 225
What's your feeling about the output ?

Take a look at the javadoc for:

- java.util.Comparator interface
- java.util.TreeSet class
- TreeSet(Comparator) constructor
[ November 02, 2005: Message edited by: Seb Mathe ]

Regards,<br />Seb<br /> <br />SCJP 1.4
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Niranjan Deshpande:
... Please give me a verbose explanation...

http://www.coderanch.com/t/251430/java-programmer-SCJP/certification/ClassCastException-any-body-plz-Explain


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19783
    
  20

Originally posted by Niranjan Deshpande:
TreeSet list = new TreeSet(new Car());

This is the only real dodgy line. This line does not add a Car (with 0 wheels) to the TreeSet, it only says: use the compare method of this Car object to compare any other Objects (Cars).
[ November 02, 2005: Message edited by: Rob Spoor ]

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

#5 is correct. the correct method for the Comparable method is public int compareTo(Object o)

the method compare(Object o1, Object o2) is the methods signature for a Comparator.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

Under the Add Reply button is a set of 10 buttons. One of them is the CODE button which adds CODE tags that you can place around code you want to post to keep the formatting.

Mark
srikanth reddy
Ranch Hand

Joined: Jul 28, 2005
Posts: 252
frnds

i have seen the doc ...i cant find the compareTo method in comparator interface..might b iam missing some where ..
the link they have provided implies that when there is no argument then it sorts by natural order i.e the object being sorted should implement comparable interface (implement the method compareTo)..thats fine ..

what i feel that ..
in Comparator interface you have compare(ob1,ob2) method and
in Comparable interface you have compareTo(ob1)

please help me in understanding this....


Thanks & Regards<br /> <br />-Srikanth
Seb Mathe
Ranch Hand

Joined: Sep 28, 2005
Posts: 225
Originally posted by Mark Spritzler:
#5 is correct. the correct method for the Comparable method is public int compareTo(Object o)

the method compare(Object o1, Object o2) is the methods signature for a Comparator.

Mark


But here, the class Car implements Comparator, so this code compiles fine...

Srikanth,

The Comparable interface is used to mark a class which can be compared with others.

A Comparator object is used for classes which can compare two objects.

A TreeSet maintains elements sorted.
Objects can be sorted in two ways, depending how you construct the TreeSet :
- If a Comparator is passed to the TreeSet constructor, this Comparator is used to compare objects.
- If no Comparator is provided, the elements must implement Comparabale so they can be compared and they are sorted this way.
srikanth reddy
Ranch Hand

Joined: Jul 28, 2005
Posts: 252
yes seb thats what i was saying ...
but i have compiled the code it is giving me 3 errors..as
class Car should be declared abstract; it does not define method compare(java.lang.Object, java.lang.Object) in interface java.util.Comparator
cannot cast Object to Car
cannot cast Object to Car

strange thing here compare method is implemented ..which is contradictory to the error message ......

please help....
Seb Mathe
Ranch Hand

Joined: Sep 28, 2005
Posts: 225
I think you've made a mistake somewhere...

I've just pasted this code in my IDE and it comiles fine without any modification.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

For some reason I thought Comparator was a class.

But either way, the TreeSet uses Comparables to add into its Set. So if you don't implement Comparable, then that Object cannot be added to a TreeSet.

Look at the TreeSet Javadoc API, it clearly states that the TreeSet does not implement the Set interface correctly. Most Sets use Equals to determine if the object already exists in the Set, but not TreeSet it uses compareTo of Comaparable, or a Comparator passed in, not a Comparator as an Object in the TreeSet.

Mark
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

Actually, here's the quote from the Javadocs.


Note that the ordering maintained by a set (whether or not an explicit comparator is provided) must be consistent with equals if it is to correctly implement the Set interface. (See Comparable or Comparator for a precise definition of consistent with equals.) This is so because the Set interface is defined in terms of the equals operation, but a TreeSet instance performs all key comparisons using its compareTo (or compare) method, so two keys that are deemed equal by this method are, from the standpoint of the set, equal. The behavior of a set is well-defined even if its ordering is inconsistent with equals; it just fails to obey the general contract of the Set interface.


Mark
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Plz xplain this code on TreeSet