Author
About Comparator Interface
Abimaran Kugathasan
Ranch Hand
Joined: Nov 04, 2009
Messages: 312
posted Feb 03, 2010 09:37:07
In K & B's book, it's said that....
..... you can use it to sort instances of any class. ...
My Question is : How it's possible?
(Can we compare by same fields of difference Class' instances)
Please Confirm? Thanks........
This message was edited 1 time. Last update was at Feb 09, 2010 09:20:40 by Bear Bibeault
SCJP 6 in progress......!!!!!!.
Ankit Garg
Bartender
Joined: Aug 03, 2008
Messages: 6389
posted Feb 03, 2010 09:55:30
The statement implies that you can use Comparator to sort instances of classes that don't implement Comparable. If you implement the type safe Comparator i.e. Comparator<Type>, then your compareTo method won't get instances of different class anyways...
SCJP 6.0 98%, SCWCD 5 98%, Javaranch SCJP FAQ , SCWCD Links
Abimaran Kugathasan
Ranch Hand
Joined: Nov 04, 2009
Messages: 312
posted Feb 09, 2010 06:53:06
Ankit Garg write :
..... your compareTo method .....
.
Is that compare() method or compareTo() method in the Comparator interface? I think, compare()???
This message was edited 1 time. Last update was at Feb 09, 2010 06:53:26 by Abimaran Kugathasan
SCJP 6 in progress......!!!!!!.
Wouter Oet
Ranch Hand
Joined: Oct 25, 2008
Messages: 501
posted Feb 09, 2010 06:57:49
The Comparator interface contains the compare method
The Comparable interface contains the compareTo Method
"Any fool can write code that a computer can understand.
Good programmers write code that humans can understand." --- Martin Fowler
Abimaran Kugathasan
Ranch Hand
Joined: Nov 04, 2009
Messages: 312
posted Feb 09, 2010 07:00:26
Thanks Wouter Oet
SCJP 6 in progress......!!!!!!.
Wouter Oet
Ranch Hand
Joined: Oct 25, 2008
Messages: 501
posted Feb 09, 2010 07:17:58
You're welcome
"Any fool can write code that a computer can understand.
Good programmers write code that humans can understand." --- Martin Fowler
Neha Daga
Ranch Hand
Joined: Oct 30, 2009
Messages: 449
posted Feb 09, 2010 09:17:17
but the compare method in Comparator makes use of compareTo method to do the comparison. That's why Ankit said compareTo method.
SCJP 1.6 96%
Abimaran Kugathasan
Ranch Hand
Joined: Nov 04, 2009
Messages: 312
posted Feb 09, 2010 09:20:52
Ok, Neha Daga.... Thanks....
BTW, Can you give me good example, ........
SCJP 6 in progress......!!!!!!.
Neha Daga
Ranch Hand
Joined: Oct 30, 2009
Messages: 449
posted Feb 09, 2010 09:27:49
read comparator from K&B all the examples make use of it.
SCJP 1.6 96%
Wouter Oet
Ranch Hand
Joined: Oct 25, 2008
Messages: 501
posted Feb 09, 2010 09:30:17
Neha Daga wrote: but the compare method in Comparator makes use of compareTo method to do the comparison. That's why Ankit said compareTo method.
That totally depends on the implementation. If I write an implementation of Comparator then it doesn't need to a Comparable implementation:
Ankit was refering to a type safe implementation. Like he said: Comparator i.e. Comparator<Type>
"Any fool can write code that a computer can understand.
Good programmers write code that humans can understand." --- Martin Fowler