• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Comparable & Comparator

 
bharat bhasin
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
 
bharat bhasin
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a lot .
Material of great help.
 
Deepika Srivastava
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Deepika Srivastava
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20661
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50175
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's the bit about many sort sequences that is the crucial point. You can sort objects on different criteria at different times.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic