why does my compare method throw exception — Comparison method violates its general contract!

后端 未结 7 1683
别那么骄傲
别那么骄傲 2020-11-30 12:49

Why does this code

public class SponsoredComparator implements Comparator {

    public boolean equals(SRE arg0, SRE arg1){
        return arg0.g         


        
相关标签:
7条回答
  • 2020-11-30 12:53

    I agreed with all answer specially with jon, but one addinal things I want to tell that we should always check for null safety in compare method so that our method never be break and it's good habit in programming for always null checking. For more info look here

    0 讨论(0)
  • 2020-11-30 12:55

    I suspect the problem occurs when neither value is sponsored. That will return 1 whichever way you call it, i.e.

    x1.compare(x2) == 1
    
    x2.compare(x1) == 1
    

    That's invalid.

    I suggest you change this:

    object1.getSponsored() && object2.getSponsored()
    

    to

    object1.getSponsored() == object2.getSponsored()
    

    in both places. I would probably actually extract this out a method with this signature somewhere:

    public static int compare(boolean x, boolean y)
    

    and then call it like this:

    public int compare(SRE object1, SRE object2) {
        return BooleanHelper.compare(object1.getSponsored(), object2.getSponsored());
    }
    

    That will make the code clearer, IMO.

    0 讨论(0)
  • 2020-11-30 12:57

    The contract between equals() and compareTo() is that when equals() returns true, compareTo() should return 0 and when equals() is false compareTo should return -1 or +1.

    BTW: I assume your compare() method is not called very often as the debug messages will use up a signficiant amount of CPU and memory.

    0 讨论(0)
  • 2020-11-30 13:09

    maybe you just have NaN values which you compare through Collections.sort... this has been a problem to me and i got that exception even having right implementation of compare(obj1, obj2) method! Check that!

    0 讨论(0)
  • 2020-11-30 13:09

    I got the same problem today in a web application. Four calls working on the same array tried to sort it at the same time, effectively messing up for each other.

    0 讨论(0)
  • 2020-11-30 13:10

    I assume that you are using JDK 7. Check the following URL:

    From http://www.oracle.com/technetwork/java/javase/compatibility-417013.html#source

    Area: API: Utilities

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

    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.

    Nature of Incompatibility: behavioral

    RFE: 6804124

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

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