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

转载:http://www.tuicool.com/articles/MZreyuv

异常信息

java.lang.IllegalArgumentException: Comparison method violates its general contract!
 at java.util.TimSort.mergeHi(TimSort.java:868)
  at java.util.TimSort.mergeAt(TimSort.java:485)
  at java.util.TimSort.mergeCollapse(TimSort.java:408)
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)
...

原因

JDK7中的Collections.Sort方法实现中,如果两个值是相等的,那么compare方法需要返回0,否则 可能 会在排序时抛错,而JDK6是没有这个限制的。

if (len2 == 0) {
    throw new IllegalArgumentException("Comparison method violates its general contract!");
}
时间: 2024-10-02 14:20:11

java-collections.sort异常Comparison method violates its general contract!的相关文章

Java 异常解决之java.lang.IllegalArgumentException: Comparison method violates its general contract!

Java 异常解决 在你的代码前加一句 System.setProperty("java.util.Arrays.useLegacyMergeSort", "true"); 我也不知道什么原理,但是解决了!!!哈哈 Java 异常解决之java.lang.IllegalArgumentException: Comparison method violates its general contract!

android java.lang.IllegalArgumentException: Comparison method violates its general contract! 问题

android  java.lang.IllegalArgumentException: Comparison method violates its general contract! 问题 java.lang.IllegalArgumentException: Comparison method violates its general contract! at java.util.TimSort.mergeLo(TimSort.java:743) at java.util.TimSort.

Comparison method violates its general contract!

今天一个群里哥们儿碰到一个异常,抛到群里求解答,他的代码如下图: 抛出的异常信息为: Java代码   java.lang.IllegalArgumentException: Comparison method violates its general contract! at java.util.TimSort.mergeHi(TimSort.java:868) at java.util.TimSort.mergeAt(TimSort.java:485) at java.util.TimSor

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

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 o

java.lang.IllegalArgumentException: Comparison method violates its general contract!

背景 16号为了统一线上服务器运行环境,将两台服务器的Tomcat6+JDK6升级到Tomcat7+JDK7,本以为很简单的事情,升级后自己验证也没问题,没想到却悲剧了.升级后,过了半小时运营就找过来反馈问题,部分角色无法登陆系统,由于异常日志没有输出,没有找到问题,无奈回滚.今天我们就来说说JDK6升级到JDK7会遇到的坑.本文为了方便搜索,就直接以异常信息作为文章标题了. 复现 回滚后,到beta环境按照线上的权限配置,复现该问题,加上了error日志输出,输出了文章标题的异常,这个异常是在

jdk7 中Collections.sort 异常

Collections.sort 异常 java.lang.IllegalArgumentException: Comparison method violates its general contract! at java.util.TimSort.mergeHi(TimSort.java:868) at java.util.TimSort.mergeAt(TimSort.java:485) at java.util.TimSort.mergeForceCollapse(TimSort.jav

java Collections.sort()实现List排序的默认方法和自定义方法

1.java提供的默认list排序方法 主要代码: List<String> list = new ArrayList();list.add("刘媛媛"); list.add("王硕");list.add("李明");list.add("刘迪");list.add("刘布"); //升序Collections.sort(list,Collator.getInstance(java.util.Lo

Java Collections.sort方法对list集合排序

1.排序测试类 package com.ljq.test; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class UserSort { public static void main(String[] args) { List<User> userList =new ArrayList<User&g

java Collections.sort()实现List排序自定义方法

方法一: package testSimple; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class testCollectionSort { public static void main(String[] args) { List<TblPowerGroup> list = new ArrayLis