JDK我们常用的有1.6/1.7/1.8.
JDK不同版本函数,类和接口不尽相同。
虽说高版本的兼容低版本的,但也未必是全部,这里举出一个例外--
Comparator
这是JDK1.8里的源码:
/**
* compares nodes for various boolean operations.
*/
abstract class Comparator
{
/**
* Compare two strings
*
*
* @param s1 First string to compare
* @param s2 Second String to compare
*
* @return Whether the strings are equal or not
*/
abstract boolean compareStrings(XMLString s1, XMLString s2);
/**
* Compare two numbers
*
*
* @param n1 First number to compare
* @param n2 Second number to compare
*
* @return Whether the numbers are equal or not
*/
abstract boolean compareNumbers(double n1, double n2);
}
/**
* Compare strings or numbers for less than.
*/
class LessThanComparator extends Comparator
{
/**
* Compare two strings for less than.
*
*
* @param s1 First string to compare
* @param s2 Second String to compare
*
* @return True if s1 is less than s2
*/
boolean compareStrings(XMLString s1, XMLString s2)
{
return (s1.toDouble() < s2.toDouble());
// return s1.compareTo(s2) < 0;
}
/**
* Compare two numbers for less than.
*
*
* @param n1 First number to compare
* @param n2 Second number to compare
*
* @return true if n1 is less than n2
*/
boolean compareNumbers(double n1, double n2)
{
return n1 < n2;
}
}
这是JDK1.6/1.7 里的源码:
public interface Comparator<T> {
/**
* 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>
*
* In the foregoing description, the notation
* <tt>sgn(</tt><i>expression</i><tt>)</tt> designates the mathematical
* <i>signum</i> function, which is defined to return one of <tt>-1</tt>,
* <tt>0</tt>, or <tt>1</tt> according to whether the value of
* <i>expression</i> is negative, zero or positive.<p>
*
* The implementor must ensure that <tt>sgn(compare(x, y)) ==
* -sgn(compare(y, x))</tt> for all <tt>x</tt> and <tt>y</tt>. (This
* implies that <tt>compare(x, y)</tt> must throw an exception if and only
* if <tt>compare(y, x)</tt> throws an exception.)<p>
*
* The implementor must also ensure that the relation is transitive:
* <tt>((compare(x, y)>0) && (compare(y, z)>0))</tt> implies
* <tt>compare(x, z)>0</tt>.<p>
*
* Finally, the implementor must ensure that <tt>compare(x, y)==0</tt>
* implies that <tt>sgn(compare(x, z))==sgn(compare(y, z))</tt> for all
* <tt>z</tt>.<p>
*
* It is generally the case, but <i>not</i> strictly required that
* <tt>(compare(x, y)==0) == (x.equals(y))</tt>. Generally speaking,
* any comparator that violates this condition should clearly indicate
* this fact. The recommended language is "Note: this comparator
* imposes orderings that are inconsistent with equals."
*
* @param o1 the first object to be compared.
* @param o2 the second object to be compared.
* @return a negative integer, zero, or a positive integer as the
* first argument is less than, equal to, or greater than the
* second.
* @throws NullPointerException if an argument is null and this
* comparator does not permit null arguments
* @throws ClassCastException if the arguments' types prevent them from
* being compared by this comparator.
*/
int compare(T o1, T o2);
/**
* Indicates whether some other object is "equal to" this
* comparator. This method must obey the general contract of
* {@link Object#equals(Object)}. Additionally, this method can return
* <tt>true</tt> <i>only</i> if the specified object is also a comparator
* and it imposes the same ordering as this comparator. Thus,
* <code>comp1.equals(comp2)</code> implies that <tt>sgn(comp1.compare(o1,
* o2))==sgn(comp2.compare(o1, o2))</tt> for every object reference
* <tt>o1</tt> and <tt>o2</tt>.<p>
*
* Note that it is <i>always</i> safe <i>not</i> to override
* <tt>Object.equals(Object)</tt>. However, overriding this method may,
* in some cases, improve performance by allowing programs to determine
* that two distinct comparators impose the same order.
*
* @param obj the reference object with which to compare.
* @return <code>true</code> only if the specified object is also
* a comparator and it imposes the same ordering as this
* comparator.
* @see Object#equals(Object)
* @see Object#hashCode()
*/
boolean equals(Object obj);
}
这里是天差地别。在1.6/1.7中还是一个interface接口类型,到1.8就变成了abstract class抽象类了。所以,请广大网友谨慎升级JDK版本。