Java 错误:比较方法违反了它的一般约定

Java error: Comparison method violates its general contract(Java 错误:比较方法违反了它的一般约定)

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

问题描述

我看到了很多关于这个的问题,并试图解决这个问题,但经过一小时的谷歌搜索和大量的试验和错误,我仍然无法修复它.我希望你们中的一些人能发现问题.

I saw many questions about this, and tried to solve the problem, but after one hour of googling and a lots of trial & error, I still can't fix it. I hope some of you catch the problem.

这是我得到的:

java.lang.IllegalArgumentException: Comparison method violates its general contract!
    at java.util.ComparableTimSort.mergeHi(ComparableTimSort.java:835)
    at java.util.ComparableTimSort.mergeAt(ComparableTimSort.java:453)
    at java.util.ComparableTimSort.mergeForceCollapse(ComparableTimSort.java:392)
    at java.util.ComparableTimSort.sort(ComparableTimSort.java:191)
    at java.util.ComparableTimSort.sort(ComparableTimSort.java:146)
    at java.util.Arrays.sort(Arrays.java:472)
    at java.util.Collections.sort(Collections.java:155)
    ...

这是我的比较器:

@Override
public int compareTo(Object o) {
    if(this == o){
        return 0;
    }

    CollectionItem item = (CollectionItem) o;

    Card card1 = CardCache.getInstance().getCard(cardId);
    Card card2 = CardCache.getInstance().getCard(item.getCardId());

    if (card1.getSet() < card2.getSet()) {
        return -1;
    } else {
        if (card1.getSet() == card2.getSet()) {
            if (card1.getRarity() < card2.getRarity()) {
                return 1;
            } else {
                if (card1.getId() == card2.getId()) {
                    if (cardType > item.getCardType()) {
                        return 1;
                    } else {
                        if (cardType == item.getCardType()) {
                            return 0;
                        }
                        return -1;
                    }
                }
                return -1;
            }
        }
        return 1;
    }
}

有什么想法吗?

推荐答案

异常消息实际上非常具有描述性.它提到的合约是传递性:if A >BB >C 然后对于任何 ABC:A >C.我用纸和铅笔检查了它,你的代码似乎有几个漏洞:

The exception message is actually pretty descriptive. The contract it mentions is transitivity: if A > B and B > C then for any A, B and C: A > C. I checked it with paper and pencil and your code seems to have few holes:

if (card1.getRarity() < card2.getRarity()) {
  return 1;

如果 card1.getRarity() > 你不返回 -1card2.getRarity().

you do not return -1 if card1.getRarity() > card2.getRarity().

if (card1.getId() == card2.getId()) {
  //...
}
return -1;

如果 id 不相等,则返回 -1.您应该返回 -11 取决于哪个 id 更大.

You return -1 if ids aren't equal. You should return -1 or 1 depending on which id was bigger.

看看这个.除了更具可读性之外,我认为它应该确实有效:

Take a look at this. Apart from being much more readable, I think it should actually work:

if (card1.getSet() > card2.getSet()) {
    return 1;
}
if (card1.getSet() < card2.getSet()) {
    return -1;
};
if (card1.getRarity() < card2.getRarity()) {
    return 1;
}
if (card1.getRarity() > card2.getRarity()) {
    return -1;
}
if (card1.getId() > card2.getId()) {
    return 1;
}
if (card1.getId() < card2.getId()) {
    return -1;
}
return cardType - item.getCardType();  //watch out for overflow!

这篇关于Java 错误:比较方法违反了它的一般约定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Java 错误:比较方法违反了它的一般约定

基础教程推荐