在 JDK 7 版本以上, Comparator 要满足自反性,传递性,对称性

在 JDK 7 版本以上, Comparator 要满足自反性,传递性,对称性,不然 Arrays . sort ,
Collections . sort 会报 IllegalArgumentException 异常。
说明:
1 ) 自反性: x , y 的比较结果和 y , x 的比较结果相反。
2 ) 传递性: x > y , y > z ,则 x > z 。
3 ) 对称性: x = y ,则 x , z 比较结果和 y , z 比较结果相同。
反例:下例中没有处理相等的情况,实际使用中可能会出现异常:
new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o1.getId() > o2.getId() ? 1 : -1;
}
}

原文地址:https://www.cnblogs.com/amos-s/p/6387439.html