Java error: Comparison method violates its general contract

前端 未结 10 2140
别跟我提以往
别跟我提以往 2020-11-22 04:17

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 ca

相关标签:
10条回答
  • 2020-11-22 05:17

    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;
    

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


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

    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!
    
    0 讨论(0)
  • 2020-11-22 05:22

    It might also be an OpenJDK bug... (not in this case but it is the same error)

    If somebody like me stumbles upon this answer regarding the

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

    then it might also be a bug in the Java-Version. I have a compareTo running since several years now in some applications. But suddenly it stopped working and throws the error after all compares were done (i compare 6 Attributes before returning "0").

    Now I just found this Bugreport of OpenJDK:

    • JDK-8210311
    • Affects Version/s: 8, 11
    • Fix Version/s: 12
    • https://bugs.openjdk.java.net/browse/JDK-8210311
    0 讨论(0)
  • 2020-11-22 05:22

    I had the same symptom. For me it turned out that another thread was modifying the compared objects while the sorting was happening in a Stream. To resolve the issue, I mapped the objects to immutable temporary objects, collected the Stream to a temporary Collection and did the sorting on that.

    0 讨论(0)
  • 2020-11-22 05:23

    Consider the following case:

    First, o1.compareTo(o2) is called. card1.getSet() == card2.getSet() happens to be true and so is card1.getRarity() < card2.getRarity(), so you return 1.

    Then, o2.compareTo(o1) is called. Again, card1.getSet() == card2.getSet() is true. Then, you skip to the following else, then card1.getId() == card2.getId() happens to be true, and so is cardType > item.getCardType(). You return 1 again.

    From that, o1 > o2, and o2 > o1. You broke the contract.

    0 讨论(0)
提交回复
热议问题