What is the difference between Comparable and Comparator ?

What is the difference between Comparable and Comparator ?

The comparable interface should be used when the current object is to be compared to objects of its type only.
The comparator should be used when some external class is taking your class as a parameter for some comparison operation and it doesn\'t know how to compare the objects and expects you to give it. You have a set of objects where you want to sort it in both ascending and decending order. Just Comparable will not help you in this. Here you can implement your own comparator and pass it to the sorting algorithm to use. 

By:Jalees
Date:2016-01-09 00:00:00

Post Your Answers


User Email:

User Name:
Answers:

Related Java Subjective Links

Java Subjective interview questions and answers for experienced and fresher