I have seen classes which implement both Comparable and Comparator. What does this mean? Why would I use one over the other?
Comparable lets a class implement its own comparison:
By comparison, Comparator is an external comparison:
In both implementations, you can still choose to what you want to be compared. With generics, you can declare so, and have it checked at compile-time. This improves safety, but it is also a challenge to determine the appropriate value.
As a guideline, I generally use the most general class or interface to which that object could be compared, in all use cases I envision... Not very precise a definition though ! :-(
Comparable<Object>
lets you use it in all codes at compile-time (which is good if needed, or bad if not and you loose the compile-time error) ; your implementation has to cope with objects, and cast as needed but in a robust way.Comparable<Itself>
is very strict on the contrary.Funny, when you subclass Itself to Subclass, Subclass must also be Comparable and be robust about it (or it would break Liskov Principle, and give you runtime errors).
Comparable
is usually preferred. But sometimes a class already implements Comparable
, but you want to sort on a different property. Then you're forced to use a Comparator
.
Some classes actually provide Comparators
for common cases; for instance, Strings are by default case-sensitive when sorted, but there is also a static Comparator
called CASE_INSENSITIVE_ORDER.
java.lang.Comparable
To implement Comparable
interface, class must implement a single method compareTo()
int a.compareTo(b)
You must modify the class whose instance you want to sort. So that only one sort sequence can be created per class.
java.util.Comparator
To implement Comparator interface, class must implement a single method compare()
int compare (a,b)
here are few differences between Comparator and Comparable I found on web :
If you see then logical difference between these two is Comparator in Java compare two objects provided to him, while Comparable interface compares "this" reference with the object specified.
Comparable in Java is used to implement natural ordering of object. In Java API String, Date and wrapper classes implement Comparable interface.
If any class implement Comparable interface in Java then collection of that object either List or Array can be sorted automatically by using Collections.sort() or Array.sort() method and object will be sorted based on there natural order defined by CompareTo method.
Objects which implement Comparable in Java can be used as keys in a sorted map or elements in a sorted set for example TreeSet, without specifying any Comparator.
site:How to use Comparator and Comparable in Java? With example
Read more: How to use Comparator and Comparable in Java? With example
My annotation lib for implementing Comparable and Comparator:
public class Person implements Comparable<Person> {
private String firstName;
private String lastName;
private int age;
private char gentle;
@Override
@CompaProperties({ @CompaProperty(property = "lastName"),
@CompaProperty(property = "age", order = Order.DSC) })
public int compareTo(Person person) {
return Compamatic.doComparasion(this, person);
}
}
Click the link to see more examples. compamatic
If you see then logical difference between these two is Comparator
in Java compare two objects provided to him, while Comparable
interface compares "this" reference with the object specified.
Comparable
in Java is used to implement natural ordering of object. In Java API String, Date and wrapper classes implement Comparable
interface.
If any class implement Comparable
interface in Java then collection of that object either List
or Array
can be sorted automatically by using Collections.sort()
or Array.sort()
method and object will be sorted based on there natural order defined by compareTo
method.
Objects which implement Comparable
in Java can be used as keys in a sorted map or elements in a sorted set for example TreeSet
, without specifying any Comparator
.