java.lang.IllegalArgumentException: Comparison method violates its general contract!



异常信息

java.lang.IllegalArgumentException: Comparison method violates its general contract!
at java.util.TimSort.mergeLo(TimSort.java:747)
at java.util.TimSort.mergeAt(TimSort.java:483)
at java.util.TimSort.mergeCollapse(TimSort.java:410)
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)

Comparison method violates its general contract
http://iamyida.iteye.com/blog/2255804

图解JDK7的Comparison method violates its general contract异常
http://blog.2baxb.me/archives/993


问题原因:

Android 手机中使用的JDK是1.7版本,而此版本中修改了Collections.sort 的排序算法导致此问题


解决方案:

1. 需要判断比对的对象是否为空(不判断也没发现有什么问题)

2. 不能返回正整数、负整数、0, 而应该返回1 , -1, 0

3. 必须保证返回值会return 这3种值

4. 必须保证满足对等3定律



你可能感兴趣的:(Android)