## Lecture 18: Comparable / Graph: 4/22

### Comparables

DJW section 6.1
#### Defining comparison operators for your own classes.

Code to define the comparison operator on strings to be just a comparison
of their lengths.

MyString.java

Now any method that is defined to work over Comparables will work over
MyStrings, such as `Comparables.sort`

Note we have also redefined "hashCode", so any method that uses hashTables
will also work, though in this case "work" is a little odd.

### Defining a method that works over Comparables

Code for an ordered list of some comparable type
GOrderedListA.java
But this will not work with extension `MyStringX` of `MyString`
MyStringX.java

You have to use instead
GOrderedList.java

### Conditions that have to be met.

Equals has to be an *equivalence relation.*
- A.equals(A) is true. (Reflexive)
- If A.equals(B) then B.equals(B). (Symmetric)
- If A.equals(B) and B.equals(C) then A.equals(C)

Then ` equals` divides the objects in the class into * equivalence
classes* (different sense of class). If two object are in the same
equivalence class, they are equal. If they are in different equivalence classes
they are not equal.
compareTo has to be a *total ordering compatable with equals *

- A.compareTo(B) == 0 if and only if A.equals(B).
- If A.equals(B) then A.compareTo(C) == B.compareTo(C).
- A.compareTo(C) and C.compareTo(A) have opposite signs.
- If A.compareTo(B) < 0 and B.compareTo(C) < 0 then A.compareTo(C) < 0.

hashfun has to be compatable with equals.

- If A.equals(B) then A.hashfun() = B.hashfun();

### Graphs