[Logo] JavaRanch » Big Moose Saloon
  Search | FAQ | Recent Topics | Hot Topics
Register / Login


Reply Bookmark it! Watch this topic JavaRanch » Forums » Professional Certification » Programmer Certification (SCJP)
 
RSS feed
 
New topic
Author

About Comparator Interface

Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Messages: 312

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 by Bear Bibeault


SCJP 6 in progress......!!!!!!.
Ankit Garg
Bartender

Joined: Aug 03, 2008
Messages: 6389

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

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 by Abimaran Kugathasan


SCJP 6 in progress......!!!!!!.
Wouter Oet
Ranch Hand

Joined: Oct 25, 2008
Messages: 501

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

Thanks Wouter Oet

SCJP 6 in progress......!!!!!!.
Wouter Oet
Ranch Hand

Joined: Oct 25, 2008
Messages: 501

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

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

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

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

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
 
 
 
Reply Bookmark it! Watch this topic JavaRanch » Forums » Professional Certification » Programmer Certification (SCJP)
 
RSS feed
 
New topic
hibernate profiler

.