谁能简单地解释一下,为什么这段代码抛出一个异常,“比较方法违反了它的一般契约!”,以及我该如何修复它?

private int compareParents(Foo s1, Foo s2) {
    if (s1.getParent() == s2) return -1;
    if (s2.getParent() == s1) return 1;
    return 0;
}

当前回答

我也遇到过同样的问题,但我解决了。

//This this your code

private int compareParents(Foo s1, Foo s2) {
    if (s1.getParent() == s2) return -1;
    if (s2.getParent() == s1) return 1;
    return 0;
}

违例是将不同的事物相互比较。

//acceptable
compare between s1.getParent() and s2.getParent()
//acceptable
compare between s1 and s2
//NOT acceptable
compare between s1 and s2.getParent()
//NOT acceptable
compare between s1.getParent() and s2

在我的代码中,我想通过地址的协调来排序。在比较器中,我错误地比较了X和Y,而不是X和X。

//My code:
    private void sortBasedOnX(){
        //addresses is a list of addresses where each address has X and Y
        addresses.sort((o1, o2) -> {

            String a = o1.getAddress().getX(); 
            String b = o2.getAddress().getY(); //<-- this is supposed to be getX

            return Integer.parseInt(a)-Integer.parseInt(b);
        });
    }
//acceptable
compare between o1.getAddress().getX() and o1.getAddress().getX()
//acceptable
compare between o1.getAddress().getY() and o1.getAddress().getY()
//NOT acceptable
compare between o1.getAddress().getX() and o1.getAddress().getY()
//NOT acceptable
compare between o1.getAddress().getX() and o1.getAddress()
//NOT acceptable
compare between o1.getAddress().getX() and o1

其他回答

即使你的compareTo在理论上具有可传递性,有时细微的bug会把事情搞糟……如浮点运算错误。这件事发生在我身上。这是我的代码:

public int compareTo(tfidfContainer compareTfidf) {
    //descending order
    if (this.tfidf > compareTfidf.tfidf)
        return -1;
    else if (this.tfidf < compareTfidf.tfidf)
        return 1;
    else
        return 0;

}   

传递属性显然持有,但由于某种原因,我得到了IllegalArgumentException。事实证明,由于浮点运算中的微小错误,舍入错误导致传递属性在不应该发生的地方中断!所以我重写了代码,考虑到真正微小的差异0,它工作:

public int compareTo(tfidfContainer compareTfidf) {
    //descending order
    if ((this.tfidf - compareTfidf.tfidf) < .000000001)
        return 0;
    if (this.tfidf > compareTfidf.tfidf)
        return -1;
    else if (this.tfidf < compareTfidf.tfidf)
        return 1;
    return 0;
}   

你的比较器是不可传递的。

假设A是B的父类,B是C的父类,因为A > B和B > C,那么A > C必须是这样。然而,如果你的比较器在A和C上被调用,它将返回零,这意味着A == C。这违反了契约,因此抛出异常。

库能够检测到这一点并让您知道,而不是表现得不稳定,这是相当不错的。

在compareParents()中满足传递性要求的一种方法是遍历getParent()链,而不是只查看直接祖先。

我也遇到过同样的问题,但我解决了。

//This this your code

private int compareParents(Foo s1, Foo s2) {
    if (s1.getParent() == s2) return -1;
    if (s2.getParent() == s1) return 1;
    return 0;
}

违例是将不同的事物相互比较。

//acceptable
compare between s1.getParent() and s2.getParent()
//acceptable
compare between s1 and s2
//NOT acceptable
compare between s1 and s2.getParent()
//NOT acceptable
compare between s1.getParent() and s2

在我的代码中,我想通过地址的协调来排序。在比较器中,我错误地比较了X和Y,而不是X和X。

//My code:
    private void sortBasedOnX(){
        //addresses is a list of addresses where each address has X and Y
        addresses.sort((o1, o2) -> {

            String a = o1.getAddress().getX(); 
            String b = o2.getAddress().getY(); //<-- this is supposed to be getX

            return Integer.parseInt(a)-Integer.parseInt(b);
        });
    }
//acceptable
compare between o1.getAddress().getX() and o1.getAddress().getX()
//acceptable
compare between o1.getAddress().getY() and o1.getAddress().getY()
//NOT acceptable
compare between o1.getAddress().getX() and o1.getAddress().getY()
//NOT acceptable
compare between o1.getAddress().getX() and o1.getAddress()
//NOT acceptable
compare between o1.getAddress().getX() and o1

在我的例子中,它是无限排序。 也就是说,首先这条线根据条件向上移动,然后这条线向下移动到相同的位置。 我在最后添加了一个条件,明确地建立了行序。

只是因为这是我得到的当我谷歌这个错误,我的问题是我有

if (value < other.value)
  return -1;
else if (value >= other.value)
  return 1;
else
  return 0;

>= other。Value(显然)实际上应该是Value > other。值,这样你就可以用相等的对象返回0。