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.
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 ]
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
Joined: Sep 28, 2005
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.
But here, the class Car implements Comparator, so this code compiles fine...
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.
Joined: Jul 28, 2005
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 ......
Joined: Sep 28, 2005
I think you've made a mistake somewhere...
I've just pasted this code in my IDE and it comiles fine without any modification.
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.
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.