What is the Comparable interface?
Comparable interface is used to sort arrays and collections of objects using collections.sort() and java.utils. Objects of the class implementing Comparable interface can be ordered. All classes implementing Comparable interface should implement the compareTo() method which has the return type as an integer. Signature of compareTo() method is as below:
int i = object1.compareTo(object2)
- If object1 < object2: The value of i returned will be negative.
- If object1 > object2: The value of i returned will be positive.
- If object1 = object2: The value of i returned will be zero.