Below are Similarity and Difference between Comparator and Comparable interfaces
#1.Both are interfaces
#2.Both are used for specifying sorting order of the objects stored in a Collection
#1.Packages they are available
Comparator interface is defined in java.util package, but Comparable interface is defined in java.lang package
#2.Methods they declare
Comparator interface declares method
public int compare (Object obj1, Object obj2) which returns a -ve, zero, or +ve integer as the first argument is less than, equal to, or greater than second respectively.
But Comparable interface declares method public int compareTo(Object obj)
#3.How to implement?
The class(whose objects need to be sorted) need to implement Comparable, and provide body for compareTo(Object obj) method.
Not the actual class(whose objects need to be sorted), but another class need to implement Comparator. Need to be sent as parameter while creating Collection, or to Collections.sort() method.