java根据比较器comparator排序异常:Comparison method violates its general contract!


背景

异常信息 代码

private void customSort(List<Customer> customers) {
          
   

        Collections.sort(customers, (c1, c2) -> c1.getActiveLevel() < c2.getActiveLevel() ? 1 :
                (c1.getActiveLevel().equals(c2.getActiveLevel()) ? (c1.getAuthStatus() > c2.getAuthStatus() ? 1 : -1) : -1));
}
public void test(int loopNum) {
          
   
        List<Customer> customers = new ArrayList<>();
        Customer c1 = new Customer();
        c1.setActiveLevel(1);
        c1.setAuthStatus(16);
        Customer c2 = new Customer();
        c2.setActiveLevel(3);
        c2.setAuthStatus(13);
        Customer c3 = new Customer();
        c3.setActiveLevel(2);
        c3.setAuthStatus(11);
        Customer c4 = new Customer();
        c4.setActiveLevel(2);
        c4.setAuthStatus(9);
        Customer c5 = new Customer();
        c5.setActiveLevel(2);
        c5.setAuthStatus(15);

        customers.add(c1);
        customers.add(c2);
        customers.add(c3);
        customers.add(c4);
        customers.add(c5);

        for (int i = 0; i < loopNum; i++) {
          
   
            customers.add(new Customer(c1));
            customers.add(new Customer(c2));
            customers.add(new Customer(c3));
            customers.add(new Customer(c4));
            customers.add(new Customer(c5));
        }

        customSort(customers);

        for (Customer customer : customers) {
          
   
            System.out.println(customer);
        }
        System.out.println("success!");
    }
@Data
public class Customer {
          
   

    private Integer activeLevel;

    private Integer authStatus;

    public Customer() {
          
   
    }

    public Customer(Customer customer) {
          
   
        this.userId = customer.getUserId();
        this.sex = customer.getSex();
        this.height = customer.getHeight();
        this.incomeNew = customer.getIncomeNew();
        this.eduLevel = customer.getEduLevel();
        this.faceScoreLevel = customer.getFaceScoreLevel();
        this.lastRequestTime = customer.getLastRequestTime();
        this.activeLevel = customer.getActiveLevel();
        this.registerTime = customer.getRegisterTime();
        this.authStatus = customer.getAuthStatus();
        this.grade = customer.getGrade();
        this.city = customer.getCity();
    }
}

测试

// 发生异常
public static void main(String[] args) {
          
   
        new PlayOnlineForVisitorsServiceImpl().test(100);
    }
// 不发生异常
public static void main(String[] args) {
          
   
        new PlayOnlineForVisitorsServiceImpl().test(2);
    }

修改customSort方法

需要考虑c1 == c2的情况

private void customSort(List<Customer> customers) {
          
   
   Collections.sort(customers, (c1, c2) -> c1.getActiveLevel() > c2.getActiveLevel() ? 1 :
                (c1.getActiveLevel().equals(c2.getActiveLevel()) ? (c1.getAuthStatus() > c2.getAuthStatus() ? -1 : (c1.getAuthStatus().equals(c2.getAuthStatus())) ? 0 : 1) : -1));
 }

重点

为什么会这个异常会有偶发性?跟着异常信息找到异常的地方。这个len2 == 0什么情况下会触发?

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/291509.html

(0)
上一篇 2022年10月19日
下一篇 2022年10月19日

相关推荐

发表回复

登录后才能评论