比较方法违反其一般合同!仅Java 7 [英] Comparison method violates its general contract! Java 7 only

查看:116
本文介绍了比较方法违反其一般合同!仅Java 7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这已经有一段时间了,并检查了我可以获得的所有先前答案,但是仍然无法解决问题.

I know this has been an issue for a while now, and checked all previously answers I could get, but still this one doesn't work.

对象船员"代表具有等级和其他项目的机组人员.应该通过比较int值'assigned_rank'进行比较,如果在两个实例中该值都相等,则布尔值'is_trainer'应该有所不同.

The object 'crew' represents crewmembers with ranks and other items. The comparison should be made by comparing 'assigned_rank', an int value, and if this value is equal in both instances, then 'is_trainer', a boolean, should make the difference.

只要与Java<一起运行,此方法就可以很好地工作. 7.但是自Java 7以来,我一直得到这个:

This method worked great as long as it was running with java < 7. But since Java 7 I keep getting this one:

java.lang.IllegalArgumentException: Comparison method violates its general contract!
at java.util.ComparableTimSort.mergeLo(ComparableTimSort.java:714)
at java.util.ComparableTimSort.mergeAt(ComparableTimSort.java:451)
at java.util.ComparableTimSort.mergeCollapse(ComparableTimSort.java:376)
at java.util.ComparableTimSort.sort(ComparableTimSort.java:182)
at java.util.ComparableTimSort.sort(ComparableTimSort.java:146)
at java.util.Arrays.sort(Arrays.java:472)
at java.util.Collections.sort(Collections.java:155)
at dormas_flightlog.Query.getCrew(Query.java:714)

这里是消息来源,其中一些潜在危险部分已经被注释掉,但是仍然无法正常工作:

Here is the source, where some potentially dangerous parts have allready been out-commented, but it still does not work:

public class crew implements Serializable, Comparable<crew> {

private static final long serialVersionUID = 36L;
private int flightID = 0;
private int assigned_rank = 25;
private boolean is_trainer = false;
...


@Override
public int compareTo(crew him) {

    int myRank = this.getAssigned_rank();
    int hisRank = him.assigned_rank;

    if (this == him) {
        return 0;
    }
    if (myRank > hisRank) {
        return 1;
    }
    if (myRank < hisRank) {
        return -1;
    }
    if (myRank == hisRank) {
//            if (is_trainer && !o.is_trainer) {
//                i = 1;
//            }
//            if (!is_trainer && o.is_trainer) {
//                i = -1;
//            }
//            if (is_trainer && o.is_trainer) {
//                i = 0;
//            }
//            if (!is_trainer && !o.is_trainer) {
//                i = 0;
//            }
        return 0;
    }

    return 0;
}

@Override
public int hashCode() {
    int hash = 7;
    hash = 31 * hash + this.assigned_rank;
    hash = 31 * hash + (this.is_trainer ? 1 : 0);
    return hash;
}

@Override
public boolean equals(Object o) {

    if (this == o) {
        return true;
    }


    int myRank = this.getAssigned_rank();
    int hisRank = 0;

    if (o instanceof crew) {
        crew him = (crew) o;
        hisRank = him.assigned_rank;
    } else {
        return false;
    }

    if (myRank > hisRank) {
        return false;
    }
    if (myRank < hisRank) {
        return false;
    }
    if (myRank == hisRank) {
//            if (is_trainer && !o.is_trainer) {
//                i = 1;
//            }
//            if (!is_trainer && o.is_trainer) {
//                i = -1;
//            }
//            if (is_trainer && o.is_trainer) {
//                i = 0;
//            }
//            if (!is_trainer && !o.is_trainer) {
//                i = 0;
//            }
        return true;
    }

    return false;
}

}

实现equals()只是解决此问题的一种尝试.给定的异常带有或不带有equals().我看不到compareTo方法如何违反其合同.非常感谢您的帮助....有一天此代码必须与Java 7兼容,我不知道如何... 谢谢

Implementing equals() was just a try to solve this problem. The given exception comes with or without equals(). I cannot see how the compareTo-method violates its contract. Any help is greatly appreciated....one day this code has to work with java 7 and I don't know how... Thanks

推荐答案

请参见:

来自 http://www.oracle.com/technetwork/java/javase/ibility-417013.html#source

区域:API:实用程序简介:更新了数组和数组的排序行为 集合可能会抛出IllegalArgumentException

Area: API: Utilities Synopsis: Updated sort behavior for Arrays and Collections may throw an IllegalArgumentException

描述:java.util.Arrays.sort和 (间接)由java.util.Collections.sort代替.新的 如果排序实现检测到,则可能抛出IllegalArgumentException 违反可比合同的可比公司.以前的 实施默默地忽略了这种情况.如果以前 行为是所希望的,您可以使用新系统 属性java.util.Arrays.useLegacyMergeSort,以还原先前的 mergesort行为.

Description: The sorting algorithm used by java.util.Arrays.sort and (indirectly) by java.util.Collections.sort has been replaced. The new sort implementation may throw an IllegalArgumentException if it detects a Comparable that violates the Comparable contract. The previous implementation silently ignored such a situation. If the previous behavior is desired, you can use the new system property java.util.Arrays.useLegacyMergeSort, to restore previous mergesort behavior.

不兼容的性质:行为

RFE:6804124

RFE: 6804124

有关更多详细信息,请参见错误数据库参考此处.

For more detailed info, see the bug database reference here.

这篇关于比较方法违反其一般合同!仅Java 7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆