- String类中的compareTo()方法
/** * Compares two strings lexicographically. * The comparison is based on the Unicode value of each character in * the strings. The character sequence represented by this * <code>String</code> object is compared lexicographically to the * character sequence represented by the argument string. The result is * a negative integer if this <code>String</code> object * lexicographically precedes the argument string. The result is a * positive integer if this <code>String</code> object lexicographically * follows the argument string. The result is zero if the strings * are equal; <code>compareTo</code> returns <code>0</code> exactly when * the {@link #equals(Object)} method would return <code>true</code>. * <p> * This is the definition of lexicographic ordering. If two strings are * different, then either they have different characters at some index * that is a valid index for both strings, or their lengths are different, * or both. If they have different characters at one or more index * positions, let <i>k</i> be the smallest such index; then the string * whose character at position <i>k</i> has the smaller value, as * determined by using the < operator, lexicographically precedes the * other string. In this case, <code>compareTo</code> returns the * difference of the two character values at position <code>k</code> in * the two string -- that is, the value: * <blockquote><pre> * this.charAt(k)-anotherString.charAt(k) * </pre></blockquote> * If there is no index position at which they differ, then the shorter * string lexicographically precedes the longer string. In this case, * <code>compareTo</code> returns the difference of the lengths of the * strings -- that is, the value: * <blockquote><pre> * this.length()-anotherString.length() * </pre></blockquote> * * @param anotherString the <code>String</code> to be compared. * @return the value <code>0</code> if the argument string is equal to * this string; a value less than <code>0</code> if this string * is lexicographically less than the string argument; and a * value greater than <code>0</code> if this string is * lexicographically greater than the string argument. */ public int compareTo(String anotherString) { // this对象所对应的字符串的长度 int len1 = value.length; // 参数对象所对应字符串的长度 int len2 = anotherString.value.length; // 取长度较小者 int lim = Math.min(len1, len2); // value是String底层的实现,为char[]类型数组 // this对象所对应的字符串 char v1[] = value; // 参数对象所对应的字符串 char v2[] = anotherString.value; int k = 0; // 遍历两个字符串 while (k < lim) { char c1 = v1[k]; char c2 = v2[k]; // 如果不相等,则返回 if (c1 != c2) { return c1 - c2; } // 继续遍历 k++; } // 一个字符串是另外一个字符串的子串 return len1 - len2; }
String类中的主要比较标准为:将字符串中的每一个字符取出来进行一一对比,如果第一个一样,就比较第二个字符,第二个一样,则比较第三个字符,依次类推。
-
Character类中的compareTo()方法
/** * The value of the <code>Character</code>. * * @serial */ private final char value; /** * Constructs a newly allocated <code>Character</code> object that * represents the specified <code>char</code> value. * * @param value the value to be represented by the * <code>Character</code> object. */ public Character(char value) { this.value = value; } /** * Compares two <code>Character</code> objects numerically. * * @param anotherCharacter the <code>Character</code> to be compared. * @return the value <code>0</code> if the argument <code>Character</code> * is equal to this <code>Character</code>; a value less than * <code>0</code> if this <code>Character</code> is numerically less * than the <code>Character</code> argument; and a value greater than * <code>0</code> if this <code>Character</code> is numerically greater * than the <code>Character</code> argument (unsigned comparison). * Note that this is strictly a numerical comparison; it is not * locale-dependent. * @since 1.2 ------------------------------自从1.2开始--------------------- */ public int compareTo(Character anotherCharacter) { return this.value - anotherCharacter.value; }
-
Boolean类中的compareTo()方法
//熟悉的一些实现加比较标准的实现: /** * The value of the Boolean. * * @serial */ private final boolean value; /** * Allocates a <code>Boolean</code> object representing the * <code>value</code> argument. * * <p><b>Note: It is rarely appropriate to use this constructor. * Unless a <i>new</i> instance is required, the static factory * {@link #valueOf(boolean)} is generally a better choice. It is * likely to yield significantly better space and time performance.</b> * * @param value the value of the <code>Boolean</code>. */ public Boolean(boolean value) { this.value = value; } /** * Returns a <tt>String</tt> object representing the specified * boolean. If the specified boolean is <code>true</code>, then * the string {@code "true"} will be returned, otherwise the * string {@code "false"} will be returned. * * @param b the boolean to be converted * @return the string representation of the specified <code>boolean</code> * @since 1.4 */ public static String toString(boolean b) { return b ? "true" : "false"; } /** * Returns a hash code for this <tt>Boolean</tt> object. * * @return the integer <tt>1231</tt> if this object represents * <tt>true</tt>; returns the integer <tt>1237</tt> if this * object represents <tt>false</tt>. */ public int hashCode() { return value ? 1231 : 1237; } /** * Compares this <tt>Boolean</tt> instance with another. * * @param b the <tt>Boolean</tt> instance to be compared * @return zero if this object represents the same boolean value as the * argument; a positive value if this object represents true * and the argument represents false; and a negative value if * this object represents false and the argument represents true * @throws NullPointerException if the argument is <tt>null</tt> * @see Comparable * @since 1.5 --------自1.5开始------------------ */ public int compareTo(Boolean b) { return (b.value == value ? 0 : (value ? 1 : -1)); //在不相等的前提下:true的话为大于 false的话为小于 }
4. java.util.Date对比较标准的实现
/** * Compares two Dates for ordering. * * @param anotherDate the <code>Date</code> to be compared. * @return the value <code>0</code> if the argument Date is equal to * this Date; a value less than <code>0</code> if this Date * is before the Date argument; and a value greater than * <code>0</code> if this Date is after the Date argument. * @since 1.2 * @exception NullPointerException if <code>anotherDate</code> is null. */ public int compareTo(Date anotherDate) { long thisTime = getMillisOf(this); long anotherTime = getMillisOf(anotherDate); return (thisTime<anotherTime ? -1 : (thisTime==anotherTime ? 0 : 1)); }
以上为源码中对同一个接口同一个方法的不同实现之间的比较。用作参考。