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!的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-13TiDB + ES:转转业财系统亿级数据存储优化实践
- 2024-05-09“2024鸿蒙零基础快速实战-仿抖音App开发(ArkTS版)”实战课程已上线
- 2024-05-09聊聊如何通过arthas-tunnel-server来远程管理所有需要arthas监控的应用
- 2024-05-09log4j2这么配就对了
- 2024-05-09nginx修改Content-Type
- 2024-05-09Redis多数据源,看这篇就够了
- 2024-05-09Google Chrome驱动程序 124.0.6367.62(正式版本)去哪下载?
- 2024-05-09有没有大佬知道这种数据应该怎么抓取呀?
- 2024-05-09这种运行结果里的10.100000001,怎么能最快改成10.1?
- 2024-05-09企业src漏洞挖掘-有意思的命令执行