public static void main(String[] args) {        
List<Long> ret = new ArrayList<>(); int n = 103000;
for(int i=0;i<n;i++){
ret.add(new Random().nextLong());
} Collections.sort( ret , new Comparator<Long>(){
@Override
public int compare(Long o1, Long o2) {
return (int) (o2-o1);
}
});
}

Exception in thread "main" java.lang.IllegalArgumentException: Comparison method violates its general contract!

at java.util.TimSort.mergeLo(TimSort.java:777)

at java.util.TimSort.mergeAt(TimSort.java:514)

at java.util.TimSort.mergeCollapse(TimSort.java:441)

at java.util.TimSort.sort(TimSort.java:245)

at java.util.Arrays.sort(Arrays.java:1512)

at java.util.ArrayList.sort(ArrayList.java:1454)

at java.util.Collections.sort(Collections.java:175)

一直以为没有什么问题,大于,小于,等于情况都考虑了。 原来原因是:integer overflow,看下面的例子就明白了。负数-正数=正数

o2:-273192312377492627

o1:3368764997970232313

o2-o1:1167472500

所以,这个compare里面定义的方法不符合compare的约束(传递性,对称性,反对称性)

PS:https://docs.oracle.com/javase/7/docs/api/java/util/Comparator.html#compare(T,%20T)

http://svn.python.org/projects/python/trunk/Objects/listsort.txt

[ Error 分析] Comparison method violates its general contract!的更多相关文章

  1. mysql性能优化及 Comparison method violates its general contract

    项目上嵌套结果集查询,查询的列表再根据每个id进行查询计算,嵌套的sql如下: SELECT SUM(IFNULL(t.out_rate,0)) totalOutRate, SUM(IF(IFNULL ...

  2. JDK7的Comparison method violates its general contract异常

    1.摘要 前一阵遇到了一个使用Collections.sort()时报异常的问题,跟小伙伴@zhuidawugui 一起排查了一下,发现问题的原因是JDK7的排序实现改为了TimSort,之后我们又进 ...

  3. 排序遇到问题 JDK7的Comparison method violates its general contract

    图解JDK7的Comparison method violates its general contract异常 楼主分析的很详细,能力有限,我看得迷迷糊糊的,不过大致知道这个错误的起因了.学习了,谢 ...

  4. 解决 Comparison method violates its general contract!

    问题:Comparison method violates its general contract!报错 Collections.sort(list, new Comparator<Integ ...

  5. java-collections.sort异常Comparison method violates its general contract!

    转载:http://www.tuicool.com/articles/MZreyuv 异常信息 java.lang.IllegalArgumentException: Comparison metho ...

  6. Comparison method violates its general contract

    生产环境出现的错误排查,错误log如下 java.lang.IllegalArgumentException: Comparison method violates its general contr ...

  7. 解决“Comparison method violates its general contract!”

    The ONE跑MaxProp.Prophet可能(取决于你JDK的版本)会报“java.lang.IllegalArgumentException: Comparison method violat ...

  8. Comparison method violates its general contract 解决

    java.lang.IllegalArgumentException: Comparison method violates its general contract! 原因 JDK7中的Collec ...

  9. 关于jdk7中 使用Collections的排序方法时报Comparison method violates its general contract!异常

    参考: Comparison method violates its general contract Comparison method violates its general contract! ...

随机推荐

  1. 写一个php小脚本辅助渗透测试

    因为一个注入要爬行一些数据,然后写的一个小脚本,能写脚本来辅助渗透,也算是里程碑.哈哈哈 <?php $num = 1; while ($num <= 39) { $web_url = & ...

  2. ext,exrReturn新增,修改删除等用

    package cn.edu.hbcf.common.vo; /** * Ext Ajax 返回对象 * * @author * @date 2012-02-21 19:30:00 * */ publ ...

  3. hdu 3836 Equivalent Sets(强连通分量--加边)

    Equivalent Sets Time Limit: 12000/4000 MS (Java/Others)    Memory Limit: 104857/104857 K (Java/Other ...

  4. Maven 安装教程

    Linux系统: 1.准本工作 Maven下载地址:http://mirror.bit.edu.cn/apache/maven/maven-3/3.3.9/binaries/apache-maven- ...

  5. 【Android Studio】之构建项目报错

    问题1: 报错: Could not download fastutil.jar (it.unimi.dsi:fastutil:7.2.0): No cached version available ...

  6. php 判断是否在微信浏览器中打开

    <?php $a=false; $b=$_SERVER['HTTP_USER_AGENT']; if(strpos($b,"MicroMessenger")===false) ...

  7. EntityFramework增删改查

    http://www.cnblogs.com/libingql/archive/2013/01/29/2881988.html

  8. Wex5循环某个div节点下的所有img标签,替换其src属性值

    Model.prototype.newsDetailAfterRefresh = function(event){ var newsDiv = this.getElementByXid("n ...

  9. Pert图简介

    活动图,即工程网络技术,又称PERT(Project Evaluation and Review Technique,PERT)技术. 参考地址: http://www.cnblogs.com/jiq ...

  10. iOS UITextView 输入内容实时更新cell的高度

    iOS UITextView 输入内容实时更新cell的高度 2014-12-26 11:37 编辑: suiling 分类:iOS开发 来源:Vito Zhang'blog  11 4741 UIT ...