However, another problem is that we couldn't generalize this function to other type of objects.
compareTo
We can create an interface that guarantees that any implementing class, like Dog, contains a comparison method, which we'll call compareTo.
Let's write our own interface.
Return a negative number if this < o.
Return 0 if this equals o.
Return a positive number if this > o.
Now, we could let our Dog class implements the OurComparable interface.
Then, we could generalize the max function by taking in OurComparable objects.
Comparables
Although OurComparable interface seems solved the issue, it's awkward to use and there's no existing classes implement OurComparable. The solution is that we could use the existed interface Comparable.
Notice that Comparable<T> means that it takes a generic type. This will help us avoid having to cast an object to a specific type.
Comparator
We could only implement one compareTo method for each class. However, if we want to add more orders of comparasion, we could implement Comparator interface.
This shows that the Comparator interface requires that any implementing class implements the compare method.
To compare two dogs based on their names, we can simply defer to String's already defined compareTo method.
public interface OurComparable {
public int compareTo(Object o);
}
public class Dog implements OurComparable {
private String name;
private int size;
public Dog(String n, int s) {
name = n;
size = s;
}
public void bark() {
System.out.println(name + " says: bark");
}
public int compareTo(Object o) {
Dog uddaDog = (Dog) o;
return this.size - uddaDog.size;
}
}
public static OurComparable max(OurComparable[] items) {
int maxDex = 0;
for (int i = 0; i < items.length; i += 1) {
int cmp = items[i].compareTo(items[maxDex]);
if (cmp > 0) {
maxDex = i;
}
}
return items[maxDex];
}
public interface Comparable<T> {
public int compareTo(T obj);
}
public class Dog implements Comparable<Dog> {
...
public int compareTo(Dog uddaDog) {
return this.size - uddaDog.size;
}
}
public interface Comparator<T> {
int compare(T o1, T o2);
}
import java.util.Comparator;
public class Dog implements Comparable<Dog> {
...
public int compareTo(Dog uddaDog) {
return this.size - uddaDog.size;
}
private static class NameComparator implements Comparator<Dog> {
public int compare(Dog a, Dog b) {
return a.name.compareTo(b.name);
}
}
public static Comparator<Dog> getNameComparator() {
return new NameComparator();
}
}