130

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


2 답변


176

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.


  • 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. - Sridhar
  • 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. - markg
  • You can use "...implements Comparator" and you can use "..implements Comparable", what's the difference in this case? - powder366

121

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.


  • Control over the source is the keyword here. - Jonas Gröger
  • @JonasGröger What do you mean by not having a control over a class? - TheLogicGuy
  • @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
  • 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

Linked


Related

Latest