Type Inference and Anonymous Classes
The Kings of Sordid: Comparable
and Comparator
Comparable<T>
specifies compareTo
This imposes the "natural ordering" on instances of the
type T
Compararator
specifies compare
Partially Ordered Sets
Caresian Product If A and B are sets, A X B is the set of all tuples (x,y) where x∈A and y∈B.
A relation R on a set S is any subset of S X S.
Let S = real numbers. Then < is the open half-plane below the line y = x. Equality is just the line y = x.
We write x R y for (x,y) ∈ R
Relations
- reflexive This means every element is related to itself. x R x for all x ∈ S
- symmetric This means that if x R y , then y R x.
- antisymmetric If (x R y) and (y R x), then x = y.
- transitiveIf x R y and y R Z, then x R z.
Three important types of relations
- partial ordering Symmetric and transitive
- linear orderingFor any x, y x R y or y R x.
- equivalence relation This is reflexive, symmmetric, and transitive.
compareTo
puts a linear ordering on objects
in a class.
x.compareTo(y) R 0 means x R y
where R is <= < > >=
compare(T x, T y)
This must impose a linear
ordering on the objects in T.
(x,y) -> x*x - y*y
x.compareTo(y) = - y.compareTo(x) is very desirable.