I have seen classes which implement both Comparable and Comparator. What does this mean? Why would I use one over the other?
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.