What is difference between Comparable and Comparator interface

What is difference between Comparable and Comparator interface

Answer: Below are the differences:

ComparableComparator
int compareTo(T o) – Compares this object (Itself) with other specified object for orderint compare(T o1,

T o2) – It compares two arguments supplied at runtime for ordering.

It returns integer, negative zero or a positive integer as the first argument is equal to, less than or greater than the secondIt also returns integer, negative zero or a positive integer as the first argument is equal to, less than or greater than the second
It compares itself with others with same type of objectIt compares two different object
To compare class itself must implements Comparable interface and override its compareTo methodHere the Comparator class must implements the Comparator interface
Comparable belongs to java.lang.* packageComparator belongs to java.util.* package

 

Leave a Reply

Your email address will not be published. Required fields are marked *