String中Comparable的compareTo使用及释义

归纳总结

  1. 字符串A(6位)、B(8位),A.compareTo(B)
    首先将A与B字符串1-6位做比较,
    第一种情况:比如第4位上分别为a、b,unicode相差1则return -1,后面的位置不做比较;
    第二种情况:1-6位都相等,则return A.length-B.lengh;
  2. 如果字符串A、B lenght相同,并且每一位都一样,则return 0;

测试代码

/**
 * @author :renpan
 * @version :v1.0
 * @class :com.luomo.addressselected
 * @date :2016-08-15 11:41
 * @description:
 */
public class Test {
    public static void main(String[] args) {
        System.out.println("\"a\".compareTo(\"b\"):"+"a".compareTo("b"));
        System.out.println("\"b\".compareTo(\"a\"):"+"b".compareTo("a"));

        System.out.println("\"ab\".compareTo(\"abc\"):"+"ab".compareTo("abc"));
        System.out.println("\"ab\".compareTo(\"abd\"):"+"ab".compareTo("abd"));
        System.out.println("\"ab\".compareTo(\"abe\"):"+"ab".compareTo("abe"));

        System.out.println("\"ab\".compareTo(\"acdef\"):"+"ab".compareTo("acdef"));
        System.out.println("\"ab\".compareTo(\"abcdef\"):"+"ab".compareTo("abcdef"));

        System.out.println("\"ab\".compareTo(\"ab\"):"+"ab".compareTo("ab"));
    }
}

输出结果

//a、b两个字符相差1
"a".compareTo("b"):-1
"b".compareTo("a"):1
//前几个字符相同,则返回相差的长度。ab、abc长度相差1
"ab".compareTo("abc"):-1
"ab".compareTo("abd"):-1
"ab".compareTo("abe"):-1
"ab".compareTo("abcdef"):-4
//第二个字符不同,则返回unicode相差的值
"ab".compareTo("acdef"):-1
//完全相同,返回0
"ab".compareTo("ab"):0

源码释义

/**
  * Compares this string to the given string.
  * 比较两个字符串
  * <p>The strings are compared one {@code char} at a time.
  * 每次比较一个一个字符
  * In the discussion of the return value below, note that {@code char} does not
  * mean code point, though this should only be visible for surrogate pairs.
  * 比较结果如下,
  * <p>If there is an index at which the two strings differ, the result is
  * the difference between the two {@code char}s at the lowest such index.
  * 两个字符串的char不同(对于短的字符串从第一个到最后一个),则返回这两个char之间相差的字符个数(如:"c".compareTo("a")返回2)
  * If not, but the lengths of the strings differ, the result is the difference
  * between the two strings' lengths.
  * 两个字符串一样,但是长度不同,则返回字符串的长度差(如:"ad".compareTo("a")返回1)
  * If the strings are the same length and every {@code char} is the same, the result is 0.
  * 两个字符串长度相同,并且每个char相同则返回0
  * @throws NullPointerException
  *             if {@code string} is {@code null}.
  */
 public native int compareTo(String string);

JDK API释义

Compares two strings lexicographically(字典顺序). The comparison(比较) is based on the Unicode value of each character in the strings.
The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argument string. 
当前字符串和参数字符串做比较
The result is a negative integer if this String object lexicographically precedes the argument string.
结果为负数,表示当前字符串在参数字符串之前,如a,b(参考源码来看lexicographically 类似的就是所有字符串的一个大集合,而且是有序的)
 The result is a positive integer if this String object lexicographically follows the argument string. 
 结果为正数,表示当前字符串在参数字符串之后,如b,a;
The result is zero if the strings are equal;  compareTo returns 0 exactly when the equals(Object) method would return true.
结果为0表示两个字符串相等;返回0时,两个字符串使用equals会返回true
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.
如果两个字符串不同,包括从左到右某一位char不同,或者说长度不同。如ab、abc;abc、def
 If they have different characters at one or more index positions, let k be the smallest such index;
 如果两个字符串有多个位置不一样,找到索引最小的位置
 then the string whose character at position k has the smaller value, as determined by using the < operator, lexicographically precedes the other string. 
 然后比较当前位置那个的unicode值小,小多少
 In this case, compareTo returns the difference of the two character values at position k in the two string -- that is, the value:
在这种情况下,compareTo返回这两个字符的差
 this.charAt(k)-anotherString.charAt(k)
 If there is no index position at which they differ, then the shorter string lexicographically precedes the longer string. In this case, compareTo returns the difference of the lengths of the strings -- that is, the value:
 this.length()-anotherString.length()
 如果位置上的字符都一样,短字符串在长字符串之前。这种情况下,会返回字符串长度的差
Specified by:
compareTo in interface Comparable<String>
Parameters:
anotherString - the String to be compared.
Returns:
the value 0 if the argument string is equal to this string; a value less than 0 if this string is lexicographically less than the string argument; and a value greater than 0 if this string is lexicographically greater than the string argument.
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值