What is the difference between compare() and compareTo() methods in Java? Do those methods give the same answer?
What is the difference between compare() and compareTo() methods in Java? Do those methods
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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 implementComparable<T>– Example:String, wrapper classes,BigIntegercompare(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 assort()or for use by sorting data structures such asTreeMapandTreeSet. 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 acompareTo()method.You will want to implement
Comparable<T>in your class if you want to use it withCollections.sort()orArrays.sort()methods.Defining a Comparator object
You can create Comparators to sort any arbitrary way for any class.
For example, the
Stringclass defines theCASE_INSENSITIVE_ORDERcomparator.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
Hashtableis 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:
Comparableis in the implementation and not visible from the interface, so when you sort you don’t really know what is going to happen.Comparatorgives you reassurance that the ordering will be well defined.