Sunday 29 May 2016

Difference between Comparable and Comparator interfaces ?



Comparable and Comparator interface are two interfaces used to compare and sort the objects.Below are some basic differences between Comparable and Comparator interface.



Comparable
Comparator
It uses the compareTo() method.
int objectOne.compareTo(objectTwo).
it uses the compare() method.

int compare(ObjOne, ObjTwo)
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 used by third-party classes to sort instances.


To know more about the Comparator,refer this example Comparator Example

Enjoy Reading

No comments:

Post a Comment