Java : Comparable vs Comparator [duplicate]
Asked Answered
R

2

160

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

Retch answered 5/11, 2010 at 17:51 Comment(7)
grdurand.com/static/presentation_four/comparable.htmlEuphorbia
Also a dup of: When to use Comparable and Comparator.Criminal
Excellent example link. Simple and clear. Do read.Hamamelidaceous
Your updated link is full of advertisement, and even cannot remove.Iridosmine
You may look at this smugjava.blogspot.in/2017/11/…Gut
Another great one: codecramp.com/java-comparable-vs-comparatorDisunion
for detailed explanation of the uses of both compare() and compareTo(): sysdotoutdotprint.com/technologies/java/3Iodous
H
213

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.

Hexad answered 5/11, 2010 at 18:9 Comment(4)
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.Oystercatcher
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.Whistle
You can use "...implements Comparator" and you can use "..implements Comparable", what's the difference in this case?Crispate
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 ?Allochthonous
H
150

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.

Holman answered 5/11, 2010 at 17:52 Comment(5)
Control over the source is the keyword here.Lananna
@JonasGröger What do you mean by not having a control over a class?Gifferd
@Gifferd You do not have control over a class if it is from a dependency or other code you cannot / are not allowed to change.Lananna
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.Ucayali
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.Serviceman

© 2022 - 2024 — McMap. All rights reserved.