Comparison method violates its general contract

简介:

   今天一个群里哥们儿碰到一个异常,抛到群里求解答,他的代码如下图:

抛出的异常信息为:

Java代码   收藏代码
  1. java.lang.IllegalArgumentException: Comparison method violates its general contract!  
  2. at java.util.TimSort.mergeHi(TimSort.java:868)  
  3. at java.util.TimSort.mergeAt(TimSort.java:485)  
  4. at java.util.TimSort.mergeCollapse(TimSort.java:408)  
  5. at java.util.TimSort.sort(TimSort.java:214)  
  6. at java.util.TimSort.sort(TimSort.java:173)  
  7. at java.util.Arrays.sort(Arrays.java:659)  
  8. 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不知道,它只要求你的逻辑必须严谨,严格考虑各种情况下两者大小的判定原则。所以正确写法应该是:

Java代码   收藏代码
  1. if(o1 == null && o2 == null) {  
  2.     return 0;  
  3. }  
  4. if(o1 == null) {  
  5.     return -1;  
  6. }  
  7. if(o2 == null) {  
  8.     return 1;  
  9. }  
  10. if(o1.getCreateTime() > o2.getCreateTime()) {  
  11.     return 1;  
  12. }  
  13. if(o2.getCreateTime() > o1.getCreateTime()) {  
  14.     return -1;  
  15. }  
  16. return 0;  
转载: http://iamyida.iteye.com/blog/2255804
目录
相关文章
GenImageInterleaved Wrong type of control parameter 1
GenImageInterleaved Wrong type of control parameter 1
解决办法:Type safety: The expression of type List needs unchecked conversion to conform
解决办法:Type safety: The expression of type List needs unchecked conversion to conform
181 0
Neither Quantity object nor its magnitude supports indexing
Neither Quantity object nor its magnitude supports indexing
SAP MM Error message - Customizing incorrectly maintained – in transaction code ML81N
SAP MM Error message - Customizing incorrectly maintained – in transaction code ML81N
SAP MM Error message - Customizing incorrectly maintained – in transaction code ML81N
2015-03-18 header note creation in my Appointment
2015-03-18 header note creation in my Appointment
78 0
2015-03-18 header note creation in my Appointment
Field creation not permitted in partner development mode
Field creation not permitted in partner development mode
Field creation not permitted in partner development mode
SAP C4C - Field creation not permitted in partner devel
Field creation not permitted in partner development mode
SAP C4C - Field creation not permitted in partner devel
How to find implementation of Requirement defined in Pricing Procedure
How to find implementation of Requirement defined in Pricing Procedure
How to find implementation of Requirement defined in Pricing Procedure
how to know which settype an assignment block is built based on
how to know which settype an assignment block is built based on
101 0
how to know which settype an assignment block is built based on
tcode SLG1 Object determination logic
tcode SLG1 Object determination logic
190 0
tcode SLG1 Object determination logic