wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Comparable & Comparator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Comparable & Comparator" Watch "Comparable & Comparator" New topic
Author

Comparable & Comparator

bharat bhasin
Greenhorn

Joined: Jul 19, 2009
Posts: 19
How is Comparable different from Comparator?
Also what how do we modify the instances of the class we implement Comparable in?
What do we do when we implement Comparator ?
Antany Vasanth
Ranch Hand

Joined: Jan 28, 2009
Posts: 43
Hello Bharat,

Refer : http://www.coderanch.com/t/268538/Programmer-Certification-SCJP/certification/Comparable-Vs-Comparator

Regards
Antany
bharat bhasin
Greenhorn

Joined: Jul 19, 2009
Posts: 19
thanks a lot .
Material of great help.
Deepika Srivastava
Greenhorn

Joined: Jul 15, 2009
Posts: 5
bharat bhasin wrote:How is Comparable different from Comparator?
Also what how do we modify the instances of the class we implement Comparable in?
What do we do when we implement Comparator ?


Hello, bharat

The following table summarizes the differences between the Comparable and Comparator interfaces:

Comparable Interface Comparator Interface

It uses the compareTo() method. It uses the compare() method.
int compare(ObjOne, ObjTwo) int objectOne.compareTo(objectTwo)

It is necessary to modify the class whose instance is A separate class can be created in order to sort instance
going to be sorted.

Only one sort sequence can be created. Many sort sequences can be created.

It is frequently used by the API classes. It it used by third-party classes to sort instances.

Product Manager at uCertify

Deepika Srivastava
Greenhorn

Joined: Jul 15, 2009
Posts: 5
bharat bhasin wrote:How is Comparable different from Comparator?
Also what how do we modify the instances of the class we implement Comparable in?
What do we do when we implement Comparator ?


The following table summarizes the differences between the Comparable and Comparator interfaces:

Comparable InterfaceComparator Interface
It uses the compareTo() method. <uc:stx>int objectOne.compareTo(objectTwo)</uc:stx>It uses the compare() method. <uc:stx>int compare(ObjOne, ObjTwo)</uc:stx>
It is necessary to modify the class whose instance is going to be sorted.A separate class can be created in order to sort the instances.
Only one sort sequence can be created. Many sort sequences can be created.
It is frequently used by the API classes.It it used by third-party classes to sort instances.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

Actually, most of the API classes have overloaded methods / constructors that take a Comparator for custom sorting as well. The TreeMap / TreeSet constructors, Collections.sort and Arrays.sort sure do.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39869
    
  28
It's the bit about many sort sequences that is the crucial point. You can sort objects on different criteria at different times.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Comparable & Comparator