简体   繁体   English

Java 7:java.lang.IllegalArgumentException:比较方法违反了其一般合同

[英]Java 7 : java.lang.IllegalArgumentException: Comparison method violates its general contract

I am in the process of upgrading our project to java 7. I ran into the Illegal Argument exception for Collections.sort(). 我正在将项目升级到java 7.我遇到了Collections.sort()的Illegal Argument异常。 I know the cause of the exception is the new Timsort in java 7 (I did go throw though all the questions raised previously on this issue). 我知道异常的原因是java 7中的新Timsort(尽管之前在此问题上提出了所有问题,但我确实抛出了)。 Now I need to modify the compare logic to overcome the exception. 现在我需要修改比较逻辑来克服异常。 Here is my compare method 这是我的比较方法

if (o1.isLookup() && !o2.isLookup()) {
    return -1;
}
if (!o1.isLookup() && o2.isLookup()) {
    return 1;
}

if (o1.dependsOn(o2)) {
    return 1;
}
if (o2.dependsOn(o1)) {
    return -1;
}
return 0;
  1. I tried overriding the equals() method , with the same logic as compare, thinking if equals and compare return the same result it should resolve the issue; 我尝试重写equals()方法,使用与compare相同的逻辑,认为如果equals和compare返回相同的结果它应该解决问题; but it didn't work as expected . 但它没有按预期工作。

  2. When I split the compare method into two with separate comparators as shown below then the sort(using both comparators) does not throw any exception. 当我将比较方法分成两个独立的比较器时,如下所示,然后排序(使用两个比较器)不会抛出任何异常。 What could be the possible reason? 可能的原因是什么?

Code Below: 代码如下:

protected Comparator<EntityWrapper> getComparator2() {      
    return new Comparator<EntityWrapper>() {
        public int compare(EntityWrapper o1, EntityWrapper o2) {
            if (o1.dependsOn(o2.entityClass)) {
                // This depends on otherWrapper
                return 1;
            }
            if (o2.dependsOn(o1.entityClass)) {
                // OtherWrapper depends on this
                return -1;
            }
            return 0;
        }
    };
}

protected Comparator<EntityWrapper> getComparator1() {
    return new Comparator<EntityWrapper>() {
public int compare(EntityWrapper o1, EntityWrapper o2) {
        if (o1.isLookup() && !o2.isLookup()) {
            return -1;
        }
        if (!o1.isLookup() && o2.isLookup()) {
            return 1;
        }
        return 0;
    };
}

It also has something to do with the version of JDK. 它还与JDK版本有关。 The implementation method in JDK 7 has been changed for which you are violating one of the 3 rules of compareTo method contract. JDK 7中的实现方法已被更改,您违反了compareTo方法合同的3条规则之一。

Look at this: 看这个:

Description: The sorting algorithm used by java.util.Arrays.sort and (indirectly) by java.util.Collections.sort has been replaced. 描述:java.util.Arrays.sort和(间接)java.util.Collections.sort使用的排序算法已被替换。 The new sort implementation may throw an IllegalArgumentException if it detects a Comparable that violates the Comparable contract. 如果新的排序实现检测到违反Comparable合同的Comparable,则可能抛出IllegalArgumentException。 The previous implementation silently ignored such a situation. 以前的实现默默地忽略了这种情况。 If the previous behaviour is desired, you can use the new system property, java.util.Arrays.useLegacyMergeSort, to restore previous mergesort behaviour. 如果需要先前的行为,则可以使用新的系统属性java.util.Arrays.useLegacyMergeSort来恢复先前的mergesort行为。

System.setProperty("java.util.Arrays.useLegacyMergeSort", "true");

Aren't you missing ".entityClass" such as: 你是不是错过了“.entityClass”,例如:

if (o1.isLookup() && !o2.isLookup()) {
    return -1;
}
if (!o1.isLookup() && o2.isLookup()) {
    return 1;
}

if (o1.dependsOn(o2.entityClass)) {
    return 1;
}
if (o2.dependsOn(o1.entityClass)) {
    return -1;
}
return 0;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.IllegalArgumentException:比较方法违反了它的一般约定,在 java7 中给出了错误 - java.lang.IllegalArgumentException: Comparison method violates its general contract giving Error in java7 Java sort抛出java.lang.IllegalArgumentException:比较方法违反了其一般合同 - Java sort throws java.lang.IllegalArgumentException: Comparison method violates its general contract Java 错误:java.lang.IllegalArgumentException:比较方法违反其一般约定 - Java Error: java.lang.IllegalArgumentException: Comparison method violates its general contract Java Rest Template throws java.lang.IllegalArgumentException: Comparison method violates its general contract - Java Rest Template throws java.lang.IllegalArgumentException: Comparison method violates its general contract java.lang.IllegalArgumentException:比较方法违反了它的一般约定! 日期 - java.lang.IllegalArgumentException: Comparison method violates its general contract! java.util.Date 错误:java.lang.IllegalArgumentException:即使使用替代方法,比较方法也违反了其常规协定 - Error: java.lang.IllegalArgumentException: Comparison method violates its general contract even using workaround 为什么我得到这个异常 java.lang.IllegalArgumentException:比较方法违反了它的一般契约 - Why did I get this exception java.lang.IllegalArgumentException: Comparison method violates its general contract 随机错误-java.lang.IllegalArgumentException:比较方法违反了其一般约定 - Random error - java.lang.IllegalArgumentException: Comparison method violates its general contract 获取java.lang.IllegalArgumentException:比较方法违反了其一般约定! 排序算法应该是100%稳定的 - Getting java.lang.IllegalArgumentException: Comparison method violates its general contract! with a sort algo that should be 100% stable 线程“AWT-EventQueue-0”中的异常java.lang.IllegalArgumentException:比较方法违反了其总契约 - Exception in thread “AWT-EventQueue-0” java.lang.IllegalArgumentException: Comparison method violates its general contract
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM