What is difference between Comparable and Comparator interface
Answer: Below are the differences:
Comparable | Comparator |
int compareTo(T o) – Compares this object (Itself) with other specified object for order | int 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 second | It 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 object | It compares two different object |
To compare class itself must implements Comparable interface and override its compareTo method | Here the Comparator class must implements the Comparator interface |
Comparable belongs to java.lang.* package | Comparator belongs to java.util.* package |