Comparable<T>
前言
* This interface imposes a total ordering on the objects of each class that
* implements it. This ordering is referred to as the class's <i>natural
* ordering</i>, and the class's <tt>compareTo</tt> method is referred to as
* its <i>natural comparison method</i>.<p>
就是说,只要实现了这个接口,一个类多个对象的本质的大小比较方式就被确定了。
请注意用词,impose/total/natural。很多集合类,被排序的时候默认就是调用这个方法。
SortedMap使用它来排key,SortedSet使用它来排值。
使用起来比Comparator<T>更加轻量。* The natural ordering for a class <tt>C</tt> is said to be <i>consistent
* with equals</i> if and only if <tt>e1.compareTo(e2) == 0</tt> has
* the same boolean value as <tt>e1.equals(e2)</tt> for every
* <tt>e1</tt> and <tt>e2</tt> of class <tt>C</tt>.
一个类的natural ordering和equal consistent是指,即对于相同的两个类e1和e2,(e1.compareTo(e2) == 0) == (e1.equals(e2))。
当然二者也有一定的区别,如果e2是null的话,e1.compareTo(e2) == 0会抛出NullPointerException,后者会返回false。强烈推荐实现compareTo方法时能够考虑到和equals的适配问题,这样可以避免一些麻烦。
* Virtually all Java core classes that implement <tt>Comparable</tt> have natural
* orderings that are consistent with equals. One exception is
* <tt>java.math.BigDecimal</tt>, whose natural ordering equates
* <tt>BigDecimal</tt> objects with equal values and different precisions
* (such as 4.0 and 4.00).<p>
除了BigDecimal以外的实现了ComparableJava的核心类基本上都实现了要求。BigDecimal的问题体现在精度上的差异。
方法
public int compareTo(T o);
* 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.
要严格按照这个大小规则编写代码。负数是小于,是以this作为基准的。* <p>It is strongly recommended, but <i>not</i> strictly required that
* <tt>(x.compareTo(y)==0) == (x.equals(y))</tt>. Generally speaking, any
* class that implements the <tt>Comparable</tt> interface and violates
* this condition should clearly indicate this fact. The recommended
* language is "Note: this class has a natural ordering that is
* inconsistent with equals."
重提这个问题,真的很严格。如果传入的参数是null,会抛出NullPointerException。
如果传入的参数不能这么比较,会抛出ClassCastException。因为主要到其实这是有模版的,意味着两个不同的类也是可以进行比较的。
Comparator<T>
前言
* A comparison function, which imposes a <i>total ordering</i> on some
* collection of objects.
同样可以当作参数传递给容器进行使用。* Note: It is generally a good idea for comparators to also implement
* <tt>java.io.Serializable</tt>, as they may be used as ordering methods in
* serializable data structures (like {@link TreeSet}, {@link TreeMap}). In
* order for the data structure to serialize successfully, the comparator (if
* provided) must implement <tt>Serializable</tt>.<p>
官方推荐comparators最好也要实现Serializable接口。
方法
int compare(T o1, T o2);
* Compares its two arguments for order. Returns a negative integer,
* zero, or a positive integer as the first argument is less than, equal
* to, or greater than the second.<p>
传入两个参数进行比较,不和具体的类进行绑定,而Comparable其中一个比较的对象是实现了该Comparable接口的对象。
一样会可能抛出NullPointerException和ClassCastException。
boolean equals(Object obj);
用于判断另一个comparator和当前这个的比较规则是否相同。
* Note that it is <i>always</i> safe <i>not</i> to override
* <tt>Object.equals(Object)</tt>.
不是非要强制覆写的,因为Object本来就有一个boolean equals(Object obj);方法。