The compareTo and compare Methods

suggest change

The Comparable<T> interface requires one method:

public interface Comparable<T> {

    public int compareTo(T other);

}

And the Comparator<T> interface requires one method:

public interface Comparator<T> {

    public int compare(T t1, T t2);

}

These two methods do essentially the same thing, with one minor difference: compareTo compares this to other, whereas compare compares t1 to t2, not caring at all about this.

Aside from that difference, the two methods have similar requirements. Specifically (for compareTo), Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object. Thus, for the comparison of a and b:

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents