Java中Comparator的使用 (后续补充)
1、 s1.compareTo(s2) 实际上是比较的ascii码
public class CompareTest { public static void main(String[] args) { ArrayList<Integer> list= new ArrayList<Integer>(); String s1 = "1"; String s2 = "2"; System.out.println(s1.compareTo(s2));//相当于s1-s2; }输出如下图 }
1)String s1 = "1"; String s2 = "2"; 长度一致时,输出-1;
当String s1 = "1"; String s2 = "3";长度一致时,输出-2;
分析可知,是用对应的ascii字符对比计算
1对应的ascii 是49 ,3对应的是51 比较第一个字符
1.1 Collections.sort默认是升序排列
1.2 Comparator中的compareTo方法默认是与Collections.sort一致的,从小到大排列
1.3改写Comparator中的compareTo方法
变成:
return o2.compareTo(o1);
输出结果为从大到小。[56, 23, 5, 4, 2, 1]
另外可以发现 o1.compareTo(o2) 中o1与o2在数组中的位置,o1表示后加进来的元素,o2表示原来的元素.
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class CompareTest {
public static void main(String[] args) {
ArrayList<Integer> list= new ArrayList<Integer>();
int [] numbers={1,4,2,5,23,56};
int len = numbers.length;
for(int i=0;i<len;i++){
list.add(numbers[i]);
}
/* Collections.sort(list);
for(int a: list) // printing the sorted list of names
System.out.println(a);*/
Collections.sort(list, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o1.compareTo(o2);
}
});
System.out.println(list);
}
}
2、纠正这篇博客:Java方法学习之路:compareTo()方法
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.
为排序将对象与指定的对象进行比较,根据比较的结果是小于,等于或者是大于而返回一个负数,零或者正数。In the foregoing description, the notation sgn(expression) designates the mathematical signum function, which is defined to return one of -1, 0, or 1 according to whether the value of expression is negative, zero or positive. The implementor must
ensure sgn(x.compareTo(y)) == -sgn(y.compareTo(x)) for all x and y. (This implies that x.compareTo(y) must throw an exception
iff y.compareTo(x) throws an exception.)
在前述中,符号sgn(expression)指出:数学函数必须根据表达式expression的值是负数,0,或者正数而返回一个-1,0,或者是1。实现该接口
者必须确保对于任意的对象x,y都有x.compareTo(y)) == -sgn(y.compareTo(x)成立。(这暗示如果x.compareTo(y)会抛出异常的话,那么
x.compareTo(x),也必须抛出一个异常。)
The implementor must also ensure that the relation is transitive: (x.compareTo(y)>0 && y.compareTo(z)>0) implies x.compareTo
(z)>0
实现者必须确保关系是传递的:即(x.compareTo(y)>0 && y.compareTo(z)>0)意味着x.compareTo(z)>0成立。Finally, the implementer must ensure that x.compareTo(y)==0 implies that sgn(x.compareTo(z)) == sgn(y.compareTo(z)), for all
z.
最后,实现者必须确保x.compareTo(y)==0可以得到对于任意的z可以使得sgn(x.compareTo(z)) == sgn(y.compareTo(z))成立
It is strongly recommended, but not strictly required that (x.compareTo(y)==0) == (x.equals(y)). Generally speaking, any
class that implements the Comparable 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."
虽然不是非常的需要,但是必须强调的是,(x.compareTo(y)==0) ==(x.equals(y))。一般说来,任何实现了Compareable接口,但是违背了这
种情况的类必须清楚的指出这一点。指出的语句如****释,该类具有和equals()方法不同的自然排序方法。
另外可以参考:java comparator 升序、降序、倒序从源码角度理解