Comparator不满足自反性错误,Comparison method violates its general contract
APP运行退出,跟踪信息
java.lang.IllegalArgumentException: Comparison method violates its general contract!
Collections.sort(idxsList);//按score升序排列
查看idxs类
public int compareTo(Idxs o) { //重写compareTo方法
return (int) (this.getConfVal()*100-o.getConfVal()*100);
}
JDK1.7开始对Comparator类进行了优化,某些情况必须返回0。
Comparator必须包含。自反性:当两个相同的元素相比,compare方法必须返回0,也就是compare(o1, o1) = 0;反对称性:如果compare(o1,o2) = 1,则compare(o2, o1)必须返回符号相反的值也就是 -1。
修改为:
public int compareTo(Idxs o) { //重写compareTo方法
if (this.getConfVal() < o.getConfVal()) {
return -1;
} else if (this.getConfVal() > o.getConfVal()) {
return 1;
} else {
return 0;
}
}
错误解决!