今天一个群里哥们儿碰到一个异常,抛到群里求解答,他的代码如下图:
抛出的异常信息为:
java.lang.IllegalArgumentException: Comparison method violates its general contract! at java.util.TimSort.mergeHi(TimSort.java:868) at java.util.TimSort.mergeAt(TimSort.java:485) at java.util.TimSort.mergeCollapse(TimSort.java:408) at java.util.TimSort.sort(TimSort.java:214) at java.util.TimSort.sort(TimSort.java:173) at java.util.Arrays.sort(Arrays.java:659) at java.util.Collections.sort(Collections.java:217)
我说是compare方法实现的问题,他死活跟我掰,说我之前代码还好好的啊。没办法,我只好根据异常信息提示去翻JDK源码,异常里提示at java.util.TimSort.mergeHi(TimSort.java:868)即TimSort类的mergeHi方法抛出的。于是我不断Google,找到了这篇帖子《why does my compare method throw exception — Comparison method violates its general contract》,根据他们的提示,我大概了解了compare方法需要返回1,-1,0即你的返回值要符合约定。
于是我又按照异常提示看了Collections的sort方法源码,如图:
继续跟踪Arrays类的sort方法:
看到这里我基本就豁然开朗了,因为抛异常的地方是在TimSort类里,说明实际走的是else分支,所以有了第一种解决方法,添加-Djava.util.Arrays.useLegacyMergeSort=true这个JVM参数,其实要真正解决这个问题,要符合规范的实现compare方法,因为他写的代码里没有考虑对象o1和对象o2为Null的情况,即当o1与o2都为null时两者大小如何判定呢,当o1为null但o2不为null时两者大小又如何判定了呢,同理当o2为null但o1不为null时两者大小又如何判定呢又不得而知,或许你又会说,我这两个对象不可能为null,但那是你认为,JVM不知道,它只要求你的逻辑必须严谨,严格考虑各种情况下两者大小的判定原则。所以正确写法应该是:
if(o1 == null && o2 == null) { return 0; } if(o1 == null) { return -1; } if(o2 == null) { return 1; } if(o1.getCreateTime() > o2.getCreateTime()) { return 1; } if(o2.getCreateTime() > o1.getCreateTime()) { return -1; } return 0;
相关推荐
主要给大家介绍了关于java中排序报:Comparison method violates its general contract异常的解决方法,文中介绍的非常详细,对大家具有一定的参考学习价值,需要的朋友们下面来一起看看吧。
城市形象符号的自动识别与比较,张云川,计佳伟,城市形象在城市管理中发挥着重要的作用。先前已有研究提出了一种自动识别目的地品牌与形象的模型。但是,现有的模型未能识别出特
经典关联规则挖掘论文
A self-comparison method with closely interleaved switching states is analyzed and used to evaluate some type-B uncertainties of an 87Rb atomic fountain clock. Free from additional frequency reference...
A low-medium frequency sound velocity measurement device and measurement result via comparison method
this is a comparison of db2 vs oracle
英文comparisonpaper范例,便于进行comparison paper的练习书写
Hex Comparison 是一款二进制文件比较与十六进制编辑工具。它可以帮助你使用二进制格式来比较两个文件。主要功能: 1.允许快速比较两个文件。 2.快速查找每一处不匹配的数据。 3.使用自定义颜色来显示不匹配数据。 4...
Comparison of VHDL Verilog and SystemVerilog VHDL Verilog and SystemVerilog的比较
This is the second energy entropy matlab code, EULER numerical analysis method, Comparison of soft threshold and hard threshold and today various threshold calculation method.
Comparison of learning disabled and general education children on the McCarthy Scales of Children's Abilities P.vyrhology in the Srhools 1980, 17. 429-436 COMPARISON OF LEARNING DISABLED AND ...
The regional plans of developed countries follow a few common patterns. Both subjects under research have many similarities. Nevertheless, regional plans analyze spaces with different development ...
comparison of academic and informal writing
Algorithm-sorting-algorithms-performance-comparison.zip,一组排序算法的性能比较它计算每个算法用于排序列表的时间:已排序的数字和未排序的数字,算法是为计算机程序高效、彻底地完成任务而创建的一组详细的准则...
The often quoted comparison that the method is a “Cinderella”, dominated by her “big sister”, the Finite Element Method, and whose beauty is hidden away, is still true and we hope that the reader...
A performance comparison of main stream network simulators, including NS2, NS3, OPNet, OMNet++.
Comparison between Genetic Algorithms and Particle Swarm Optimization,主要介绍了遗传算法和粒子群算法的异同。
ndvi land use /Comparison of Landscape Pattern–Normalized Difference Vegetation
Digital Change Detection by Post-Classification Comparison of RS Data in Land Use of Guangzhou,樊风雷,Wang Yunpeng,Remote sensing has long time been an important component of regional planning for ...
atlassian-visual-comparison-2.5.4-jira20.jar webdriver