159

Possible Duplicates:
difference between compare() and compareTo()
Java: What is the difference between implementing Comparable and Comparator?

What are the keys differences between Comparable and Comparator.

and which is preferred over the other in what scenarios?

Thanks

Updated - GOOD LINK WITH EXAMPLE!!

http://www.digizol.com/2008/07/java-sorting-comparator-vs-comparable.html

Community
  • 1
  • 1
daydreamer
  • 87,243
  • 191
  • 450
  • 722

2 Answers2

212

When your class implements Comparable, the compareTo method of the class is defining the "natural" ordering of that object. That method is contractually obligated (though not demanded) to be in line with other methods on that object, such as a 0 should always be returned for objects when the .equals() comparisons return true.

A Comparator is its own definition of how to compare two objects, and can be used to compare objects in a way that might not align with the natural ordering.

For example, Strings are generally compared alphabetically. Thus the "a".compareTo("b") would use alphabetical comparisons. If you wanted to compare Strings on length, you would need to write a custom comparator.

In short, there isn't much difference. They are both ends to similar means. In general implement comparable for natural order, (natural order definition is obviously open to interpretation), and write a comparator for other sorting or comparison needs.

Andy
  • 8,841
  • 8
  • 45
  • 68
  • 15
    Comparable should be implemented inside the object. So there is a dependency created with the compareTo method for a object which will be implied to a particular kind of implementation of comparing object.But,comparator is externalized and we can have multiple type of comparator for the same object. Also need corrections for my understanding. – rolling stone Apr 21 '16 at 02:52
  • A case I've encountered, is that you might want to conditionally sort on any fields at random, in which case you can pass Comparator dynamically in order to sort a collection of the class, but if you simply want to define uniqueness on another property in your class, then Implement Comparable inside the class. They are not mutually exclusive. – Mark Giaconia Nov 17 '17 at 14:14
  • 1
    You can use "...implements Comparator" and you can use "..implements Comparable", what's the difference in this case? – powder366 Aug 03 '18 at 22:54
  • it is also possible to be implemented 'compareTo' method of Comparable interface in a way which I impose to be not in natural ordering. Therefore there is nothing to do which I can do with Compartor but cannot be implemented with compareTo , is not it ? – nevzatseferoglu Apr 25 '20 at 01:39
149

Comparator provides a way for you to provide custom comparison logic for types that you have no control over.

Comparable allows you to specify how objects that you are implementing get compared.

Obviously, if you don't have control over a class (or you want to provide multiple ways to compare objects that you do have control over) then use Comparator.

Otherwise you can use Comparable.

Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
  • 55
    **Control over the source** is the keyword here. – Jonas Gröger Jul 18 '15 at 18:21
  • @JonasGröger What do you mean by not having a control over a class? – TheLogicGuy Dec 22 '16 at 18:27
  • @TheLogicGuy You do not have control over a class if it is from a dependency or other code you cannot / are not allowed to change. – Jonas Gröger Dec 22 '16 at 18:55
  • 2
    You can't add code to classes that you're including from external libraries. Comparable will only work for classes that you create yourself, since you'll need to be able to add the compareTo() method inside the class body. – Webber May 07 '17 at 22:02
  • You don't have control over the `String` class as it is already defined by Java Language Specification and hence for any custom ordering over `String` we need to use a `Comparator` instead of a `Comparable`. – iCantC Feb 26 '21 at 05:56