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

后端 未结 7 1725
别那么骄傲
别那么骄傲 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 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.

提交回复
热议问题