An object which has the type Comparable<T>
is naturally ordered with respect to T
. Given a T that
, it determines the relation (<
, =
, or >
) between this
and that
.
A Comparator<T>
is an ordering of T
. Given two T
s, it determines their relation (<
, =
, or >
).
An example of a naturally ordered type is Integer
. The natural order of integers is {-MAX_VALUE
, ..., -1
, 0
, 1
, ..., MAX_VALUE
}. A type can only have one natural order, partly because the concept of a natural order entails that it is unique, and partly because a single class can't have multiple implementations of Comperable.compareTo
.
On the other hand, there can be several meaningful Comparator
s for a single type. For example, String.CASE_INSENSITIVE_ORDER
is a Comparator<String>
which ignores case.