项目运行期间出现Comparison method violates its general contract!异常,网上查阅了一下,原因还是比较明确的:

  1. Collections.sort(list, new Comparator<Integer>() {
  2. @Override
  3. public int compare(Integer o1, Integer o2) {
  4. return o1 > o2 ? 1 : -1;// 错误的方式
  5. }
  6. });

查看代码写的本身是问题的,第4行没有考虑o1 == o2的情况,

这样在JDK6中运行是没有问题,但是在JDK7中就可能会出现上述异常。

原因是:这是因为JDK7底层的排序算法换了,而新的算法必须满足如下约束条件:

  • sgn(compare(x, y)) == -sgn(compare(y, x))
  • ((compare(x, y)>0) && (compare(y, z)>0)) implies compare(x, z)>0
  • compare(x, y)==0 implies that sgn(compare(x, z))==sgn(compare(y, z)) for all z

简单点说,就是必须指明相等的情况下返回 0.

感谢大神,查看的CSDN博客原文如下:

Comparison method violates its general contract!

Comparison method violates its general contract! 异常原因的更多相关文章

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

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

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

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

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

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

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

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

  5. Comparison method violates its general contract 解决

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

  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!

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

  9. [ Error 分析] Comparison method violates its general contract!

    public static void main(String[] args) { List<Long> ret = new ArrayList<>(); int n = 103 ...

随机推荐

  1. 三读bootmem【转】

    转自:http://blog.csdn.net/lights_joy/article/details/2704788 版权声明:本文为博主原创文章,未经博主允许不得转载.   目录(?)[-] 11  ...

  2. 标准C程序设计七---104

    Linux应用             编程深入            语言编程 标准C程序设计七---经典C11程序设计    以下内容为阅读:    <标准C程序设计>(第7版) 作者 ...

  3. iOS应用内跳转百度高德苹果地图

    移动开发经常用到基于位置的一些导航功能,但是对于对导航功能依赖性不强的的应用,我们直接采用应用外跳转地图APP即可. 但是应用间跳转,首先需要设置白名单, 在iOS 9 下涉及到平台客户端跳转,系统会 ...

  4. window.getComputedStyle——ref

    componentDidMount() { const LeftHeight = window.getComputedStyle(this.leftDom).height; console.log(L ...

  5. HAXM 6.0.5显示不兼容Windows

    HAXM 6.0.5显示不兼容Windows 最近更新Android后,用户会在Android Manager中发现,以前可以安装Intel x86模拟器现在不能安装了.提示错误信息如下:intel  ...

  6. Nginx日志参数、location匹配规则、设置密码

    1.三个参数 a)$http_referer:记录此次请求是从哪个链接访问过来的: 是直接访问,还是从其他网站跳转过来的. 例如:访问:http://www.etiantian.com/,其页面首页是 ...

  7. Cef应用程序结构

    Application Structure 应用程序结构 Every CEF3 application has the same general structure. Provide an entry ...

  8. 使用UltraISO为U盘或内存卡制作系统安装工具

    此软件可以为U盘制作Windows安装版的启动工具,也可以为Linux制作启动工具,尤其是Ubuntu这些.提示:推荐购买一些读取速度快一些的U盘,运行起来可以节省很多时间. 如果是内存卡要实现此功能 ...

  9. go 依赖包管理工具gb安装报错

    尝试了下gb工具,发现有个问题: [root@etcd1 test]# go get github.com/constabulary/gb/... /home/gopath/src/github.co ...

  10. Qt中QVector与QList的应用

    首先來看看QVector 的基本使用方式,建立一個可容納兩個元素的QVector ,並使用索引方式存取元素值:QVector<double> vect(2); vect[0] = 1.0; ...