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
目录
相关文章
|
8月前
|
Java 数据库连接 Apache
Correct the classpath of your application so that it contains compatible versions of the classes com
Correct the classpath of your application so that it contains compatible versions of the classes com
“Could not find suitable distribution for Requirement.parse(‘XXXX‘)”的问题
“Could not find suitable distribution for Requirement.parse(‘XXXX‘)”的问题
406 0
|
4月前
|
Java
flyway报错Correct the classpath of your application so that it contains compatible versions of the
flyway报错Correct the classpath of your application so that it contains compatible versions of the
101 1
|
8月前
|
JavaScript 程序员 Swift
The compiler is unable to type-check this expression in reasonable time; try breaking up the express
The compiler is unable to type-check this expression in reasonable time; try breaking up the express
92 0
Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC
Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC
|
iOS开发
Xcode报错Expected selector for Objective-C and Expected method body
Xcode报错Expected selector for Objective-C and Expected method body
228 0
Xcode报错Expected selector for Objective-C and Expected method body
解决办法:Type safety: The expression of type List needs unchecked conversion to conform
解决办法:Type safety: The expression of type List needs unchecked conversion to conform
349 0
|
Web App开发 Java
conflicts with existing, non-compatible bean definition of same name and class
使用 Idea CE 创建的 Maven Web 项目,启动时却输出以下错误: Connected to the target VM, address: '127.0.0.1:52165', transport: 'socket'  .
2254 0
PAT (Advanced Level) Practice - 1119 Pre- and Post-order Traversals(30 分)
PAT (Advanced Level) Practice - 1119 Pre- and Post-order Traversals(30 分)
133 0
PAT (Advanced Level) Practice - 1119 Pre- and Post-order Traversals(30 分)