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

2022/10/20 1:24:55

本文主要是介绍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什么情况下会触发?



这篇关于java根据比较器comparator排序异常:Comparison method violates its general contract!的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程