Comparable:
- In Java java.lang.Comparable is an interface which has compateTo() method.
- In general a class which implements Comparable interface will be comparable - meaning it can be sorted using Collection.sort() method. It is the natural ordering of the object.
- Objects which implement Comparable can be used as keys in TreeMap or TreeSet without implementing any other interface.
- For e.g. String implements Comparable interface by default.
- Class need not implement the Comparator interface to be sorted.
- A comparator is capable of comparing two different objects.
- Comparator must implement java.util.Comparator which has compare() method.
- If you are not the original author of a class, you can still sort it by implementing your own custom Comparator without modifying the original class.