equals() checks if two objects are the same or not and returns a boolean. compareTo() (from interface Comparable) returns an integer. It checks which of the two objects is "less than", "equal to" or "greater than" the other.
The compareTo() method compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings. The method returns 0 if the string is equal to the other string.
Comparable interface is used to sort the objects with natural ordering. Comparator in Java is used to sort attributes of different objects. Comparable interface compares “this” reference with the object specified. Comparator in Java compares two different class objects provided.
Both the compareToIgnoreCase() and compareTo() methods compare two strings lexicographically. The only difference is that unlike the compareTo() method, the case of the strings being compared is ignored by the compareToIgnoreCase() method.
From JavaNotes:
a.compareTo(b)
:
Comparable interface : Compares values and returns an int which tells if the values compare less than, equal, or greater than.
If your class objects have a natural order, implement the Comparable<T>
interface and define this method. All Java classes that have a natural ordering implement Comparable<T>
- Example: String
, wrapper classes, BigInteger
compare(a, b)
:
Comparator interface : Compares values of two objects. This is implemented as part of the Comparator<T>
interface, and the typical use is to define one or more small utility classes that implement this, to pass to methods such as sort()
or for use by sorting data structures such as TreeMap
and TreeSet
. You might want to create a Comparator object for the following:
sort()
method.If your class objects have one natural sorting order, you may not need compare().
Summary from http://www.digizol.com/2008/07/java-sorting-comparator-vs-comparable.html
Comparable
A comparable object is capable of comparing itself with another object.
Comparator
A comparator object is capable of comparing two different objects. The class is not comparing its instances, but some other class’s instances.
Use case contexts:
Comparable interface
The equals method and ==
and !=
operators test for equality/inequality, but do not provide a way to test for relative values.
Some classes (eg, String and other classes with a natural ordering) implement the Comparable<T>
interface, which defines a compareTo()
method.
You will want to implement Comparable<T>
in your class if you want to use it with Collections.sort()
or Arrays.sort()
methods.
Defining a Comparator object
You can create Comparators to sort any arbitrary way for any class.
For example, the String
class defines the CASE_INSENSITIVE_ORDER
comparator.
The difference between the two approaches can be linked to the notion of:
Ordered Collection:
When a Collection is ordered, it means you can iterate in the collection in a specific (not-random) order (a Hashtable
is not ordered).
A Collection with a natural order is not just ordered, but sorted. Defining a natural order can be difficult! (as in natural String order).
Another difference, pointed out by HaveAGuess in the comments:
Comparable
is in the implementation and not visible from the interface, so when you sort you don't really know what is going to happen. Comparator
gives you reassurance that the ordering will be well defined. compareTo()
is from the Comparable
interface.
compare()
is from the Comparator
interface.
Both methods do the same thing, but each interface is used in a slightly different context.
The Comparable interface is used to impose a natural ordering on the objects of the implementing class. The compareTo()
method is called the natural comparison method. The Comparator interface is used to impose a total ordering on the objects of the implementing class. For more information, see the links for exactly when to use each interface.
Similarities:
Both are custom ways to compare two objects.
Both return an int
describing the relationship between two objects.
Differences:
The method compare()
is a method that you are obligated to implement if you implement the Comparator
interface. It allows you to pass two objects into the method and it returns an int
describing their relationship.
Comparator comp = new MyComparator();
int result = comp.compare(object1, object2);
The method compareTo()
is a method that you are obligated to implement if you implement the Comparable
interface. It allows an object to be compared to objects of similar type.
String s = "hi";
int result = s.compareTo("bye");
Summary:
Basically they are two different ways to compare things.
The methods do not have to give the same answers. That depends on which objects/classes you call them.
If you are implementing your own classes which you know you want to compare at some stage, you may have them implement the Comparable interface and implement the compareTo() method accordingly.
If you are using some classes from an API which do not implement the Comparable interface, but you still want to compare them. I.e. for sorting. You may create your own class which implements the Comparator interface and in its compare() method you implement the logic.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With