Comparison method violates its general contract!
程序员文章站
2024-02-22 23:48:52
...
异常信息
java.lang.IllegalArgumentException: Comparison method violates its general contract!
at java.util.TimSort.mergeLo(TimSort.java:747)
at java.util.TimSort.mergeAt(TimSort.java:483)
at java.util.TimSort.mergeCollapse(TimSort.java:410)
at java.util.TimSort.sort(TimSort.java:214)
at java.util.TimSort.sort(TimSort.java:173)
at java.util.Arrays.sort(Arrays.java:659)
at java.util.Collections.sort(Collections.java:217)
at com.sto.customerapp.yiqianshou.servlet.CourierInfoProcess.run(CourierInfoProcess.java:77)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
原因分析
JDK1.7中修改了 Array.sort的算法.不再使用之前MergeSort ,而是使用新的TimeSort,即要满足以下三个特性:
1) 自反性:x,y 的比较结果和 y,x 的比较结果相反。
2) 传递性:x>y,y>z,则 x>z。
3) 对称性:x=y,则 x,z 比较结果和 y,z 比较结果相同。
java.util.Arrays#sort(T[], java.util.Comparator<? super T>)
public static <T> void sort(T[] a, Comparator<? super T> c) {
if (LegacyMergeSort.userRequested)
legacyMergeSort(a, c);
else
TimSort.sort(a, c);
}
解决方案
- ComparatorTool 类,重写Comparator的compare方法考虑要全面
public class ComparatorTool implements Comparator<CourierInfo> {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@Override
public int compare(CourierInfo t1, CourierInfo t2) {
if ((t1==null && t2==null) ||
(StringUtils.isBlank(t1.getSignTime()) && StringUtils.isBlank(t2.getSignTime()))){
return 0;
}
if(t1==null || StringUtils.isBlank(t1.getSignTime())){
return 1;
}
if(t2==null || StringUtils.isBlank(t2.getSignTime())){
return -1;
}
Date d1, d2;
try {
d1 = format.parse(t1.getSignTime());
d2 = format.parse(t2.getSignTime());
} catch (Throwable e) {
// 解析出错,则不进行排序
return 0;
}
return d2.compareTo(d1);
}
}
- 业务方法调用
List<CourierInfo> courierInfoList = courierInfoConfig.getCourierInfoService().listCourierInfoList(courierInfoMap);
ComparatorTool c = new ComparatorTool();
// 按时间倒叙排序
Collections.sort(courierInfoList, c);
下一篇: visual studio code简介
推荐阅读
-
Comparison method violates its general contract
-
Comparison method violates its general contract!
-
Comparison method violates its general contract!
-
Comparison method violates its general contract!
-
Comparison method violates its general contract
-
java中排序报:Comparison method violates its general contract异常的解决
-
java中排序报:Comparison method violates its general contract异常的解决
-
java 排序报:Comparison method violates its general contract异常的解决方法
-
java 排序报:Comparison method violates its general contract异常的解决方法
-
Comparison method violates its general contract!