美文网首页
compareTo()/compare

compareTo()/compare

作者: 甜甜的清风啊 | 来源:发表于2016-12-15 21:23 被阅读0次

先说明一下其用途,这两个方法都是用来排序用的。见名也应知其义,与什么比较

compareTo():String类的方法

 public native int compareTo(String string);

public int compareTo (String string)

Added in API level 1

Compares this string to the given string. The strings are compared one char at a time.

In the discussion of the return value below, note that char does not mean code point, though this should only be visible for surrogate pairs.

If there is an index at which the two strings differ, the result is the difference between the two chars at the lowest such index. If not, but the lengths of the strings differ, the result is the difference between the two strings' lengths. If the strings are the same length and every char is the same, the result is 0.

Throws NullPointerException if string is null.

我的翻译:

调用此方法的字符串和传进来的字符串比较。挨次(挨着一个一个)比较每一字符。

结果(返回值)有多种情况:

如果两个字符串完全一样(长度和每个位置上的字符),返回0。
例如:"ad".compreTo("ad"); return 0;

如果当某个位置上字符不一样,则返回其int值之差。
例如:"ad".compreTo("ab); return (int)d - (int)b = 2

如果当长度不一样,而已有的字符全都一样,则返回长度之差。
例如:"ad".compreTo("adc"); return 2 - 4 = -2;

都是前面减去后面。

compare():Comparetor<T>接口的方法

public int compare(T lhs, T rhs);

public int compare(T lhs, T rhs)

Description copied from interface:

Comparator Compares the two specified objects to determine their relative ordering. The ordering implied by the return value of this method for all possible pairs of (lhs, rhs) should form an equivalence relation.

This means that
compare(a,a) returns zero for all a the sign of compare(a,b) must be the opposite of the sign of compare(b,a) for all pairs of (a,b) From compare(a,b) > 0 and compare(b,c) > 0 it must follow compare(a,c) > 0 for all possible combinations of (a,b,c) Specified by: compare in interface Comparator Parameters: lhs - an Object. rhs - a second Object to compare with lhs.

Returns:

 an integer < 0 if lhs is less than rhs, // 返回值小于0,则lhs<rhs
 0 if they are equal,                    // 返回值为0,则lsh与rhs相等
 and > 0 if lhs is greater than rhs.a    // 返回值大于0,则lhs>rhs
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(4);
list.add(3);
Collections.sort(list, new Comparator<Integer>() {
    @Override
  public int compare(Integer lhs, Integer rhs) {
  Log.e(TAG, "compare: " + lhs + "," + rhs );
  // 分别是 2,1。得出最小的1,放在最前面。                           1 <- 2
  // 然后是 4,2。4 > 2,放在后面。                                 1, 2 <- 4
  // 然后是 3,4。3 < 4,放在4前面。然后判断是否还可以往前。           1, 2, 4 <- 3
  // 3和2比,3 > 2, 3放在2后面。结束比较
  // 结果为1, 2, 3, 4。
 return lhs.compareTo(rhs);
  // l -> r asc 升序
  // r -> l desc 降序
  }
});

相关文章

  • compareTo()/compare

    先说明一下其用途,这两个方法都是用来排序用的。见名也应知其义,与什么比较。 compareTo():String类...

  • Java重写sort排序

    实现Comparable接口,重写compareTo()方法 在sort方面里面重写compare方法

  • Java中的equals(),==,compareTo()和co

    更多内容请关注我的个人博客: Java中的equals(),==,compareTo()和compare() 首先...

  • Java 比较(==, equals, compareTo, c

    在Java中,有 ==, equals(), compareTo(), compare() 等方法可以比较两个值或...

  • 10-17学习总结

    今天学习了: 1比较字符串 Compare CompareTo Equals方法用于判断两个字符串是否有相同的值 ...

  • 如何对集合类型进行排序

    Compare方法的参数为要进行比较的两个同类型对象,返回值为int类型,返回值处理规则与CompareTo方法相...

  • 理解String的compareTo()方法

    public int compareTo(String anotherString) compareTo()的返回...

  • compareTo

    String类中是如何实现compareTo方法的 首先取出两个字符串的长度,比较较小的长度内,两者是否相等。 按...

  • compare 与CompareTo谁在前面谁在后面

    https://bbs.csdn.net/topics/390844991 总结:想要小的排在前面就用 第一个参数...

  • java day 5

    CompareTo()与Equlas()之间的区别。使用CompareTo()方法,那么这个类就要实现ICompa...

网友评论

      本文标题:compareTo()/compare

      本文链接:https://www.haomeiwen.com/subject/dfigmttx.html