Login with  Log in with facebook
Hiring Manager? SIGN UP HERE
4

What is the difference between Comparator and Comparator interface.

Rishi Yadav
10/25/2011 at 18:00
4 Answers
1
2

By using comparable interface we should implement comapreTo() method while in comparator interface we should implement compare() method. In comparable interface it is necessary to modify the class whose instace is going to be sorted while in comparator interface there is seperate class in order to sort the instace.

Avani Shah
10/25/2011 at 18:03

2
2

Comparator is used to sort explicitly i.e external sorting

Comparable is used for natural sorting

Sunita Priyadarsini Pokkunuri
10/25/2011 at 18:15

3
2

Comparable :

Is used to sort the elements in a Collection naturally. The object by itself decides the criteria of sorting. This is done by overriding the method "compareTo" of Comparable Interface. 

 

Comparator :

Is used for external sorting of the elements of a Collection. Here some external object decides the criteria of sorting. This is done by overriding the compare method of Comparator Interface. 

Meera Bhaskaran
10/26/2011 at 00:45

4
0

All classes, which have a natural ordering would implement the Comparable interface, whereas Comparator is used to define different ways of ordering.

 

Pravin Jain
01/25/2012 at 21:39

If you want to post any answer to this forum then you need to log in.
Schedule a Demo

Schedule a Demo with us

Name *
Email *
Phone *
Company *
Details