6 Dec 2017

What is difference between Comparable and Comparator?

Comparable
Comparator
Comparable meant for default natural sorting order.
Comparable meant for customized sorting order.
Present in java.lang package
Present in java,lang.util package
Defines Only One Method compareTo().
Contains only two Methods compare() and equals().
All Wrapper Classes and String Class implements Comparable Interface.
The Only implemented Classes of Comparator are Collator and RuleBaseCollator.
We can sort the list elements of Comparable type by Collections.sort(List) method.
We can sort the list elements of Comparator type by Collections.sort(List,Comparator) method.