Difference between Comparator vs Comparable in Java

Differences between Comparable and Comparator in Java

Here is the main difference between Java Comparable and Comparator:

Method used in Comparable

The following is an important method used in the Comparable interface:

CompareTo():

CompareTo() method is used to perform natural sorting on string. The meaning of natural sorting is the sort order which applies on the object, e.g., numeric order for sorting integers, alphabetical order for String, etc. The syntax of CompareTo() method is as follows: In the above syntax, T signifies the type of objects you are going to compare. CompareTo() method compares the object with T obj. Output:

It returns 0 if the values are equal. In case, if the object has a lesser value, then this method returns a negative value. If the object has a higher value, it returns a positive value.

CompareTo() Method Example:

The following program of Java comparable example shows the comparison of two characters, “a” and “b”. Character “a” comes before “b” alphabetically. Therefore, output is -1. Character “b” comes after “a” alphabetically. Hence output is 1. Character “a” and “b” both are equivalent. Hence output is 0. Output

Method used in Comparator

Following are the important methods used in comparator interface:

Compare():

Compare() enables you to order objects. To do this, you have to create a class that implements comparator interface. After this, you need to override it’s compare method. The syntax of compare() method is as follows: In the above syntax, obj1 and obj2 are two objects that you have to compare using compare() method. Output:

It returns a negative integer if the first argument is less than the second one. Returns zero if the first argument and second argument is equal. This method can return a positive integer, in case the first argument is greater than the second.

You have to ensure that the relation is transitive. For example, ((compare(a, b)>0) && (compare(b, c)>0)) which implies compare(a, c)>0.

Compare Method Example:

In the below program of Java comparator example, there are 6 variables. “x”, “y”, “p”, “q”, “r”, and “s”. The output is -1 as the value of “x”, 20 is less than the value of “y”, 30. The output is 0 because the value of “p”, 40 is equal to the value of “q”, 40. Output:

Equals():

Equals() verifies whether the number object is equal to the object, which is passed as an argument or not. The syntax of the equals() method is as follows: public boolean equals(Object o) This method takes two parameters 1) any object 2) return value. It returns true if the passed argument is not null and is an object of the similar type having the same numeric value.

Equals Method Example:

In the below example, we are comparing the four variables with each other. Three variables are of type integer and one of type short. The first result is false as the value of p, which is 5, is not equal to the value of q, which is 20. The second result is true because the value of variable p and r is the same, which is 5. Lastly, the output is false, as variable p and s have different assigned values. Output