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

Source: Internet
Author: User

Background

Number 16th in order to unify the server running environment, the two server tomcat6+jdk6 upgrade to TOMCAT7+JDK7, this thought very simple things, upgrade their own verification also no problem, did not expect tragedy. After the upgrade, half an hour after the operation to find the feedback problem, some of the role can not log on the system, due to abnormal log output, did not find the problem, helpless rollback. Today we'll talk about JDK6 upgrade to JDK7 will encounter the pit. In order to facilitate the search, this article directly with the exception information as the title of the article.

Repetition

After the rollback, to the beta environment in line with the permission configuration, to reproduce the problem, plus the error log output, the output of the article title of the exception, the exception is thrown in the code similar to the following:

Collections.sort (list, new comparator<integer> () {@Overridepublic int compare (integer o1, integer o2) {return O1 &G T O2? 1: -1;//the Wrong Way}});
Solution Solutions

First say how to solve, there are two ways to solve.

Modify Code

The above code write itself has a problem, the 4th line does not consider O1 = = O2 situation, moreover said we do not need to compare, modify the following code can be:

Collections.sort (list, new comparator<integer> () {@Overridepublic int compare (integer o1, integer o2) {//return O1 > O2? 1: -1;return o1-o2;//the Right way});
Do not modify the code

So here's the problem. Why does the above code run without problems in JDK6, while in JDK7 it throws an exception? This is because the JDK7 bottom of the sorting algorithm, if you want to continue to use the JDK6 sorting algorithm, you can add the following parameters in the JVM's startup parameters:

-djava.util.arrays.uselegacymergesort=true
This will be the same as the use of JDK6 sorting algorithm, in the case can not modify the code, to solve this compatibility problem.

Analysis

In my previous cognition, the higher version of the JDK was compatible with the previous code, discussed with colleagues plus a search, it turns out that JDK6 to JDK7 does have a compatibility problem (incompatible list). In the incompatible list we can find an incompatible description of Collections.sort, as follows:

Area:API:UtilitiesSynopsis:Updated sort behavior for Arrays and collections could throw an ILLEGALARGUMENTEXCEPTIONDESCRI Ption:the sorting algorithm used by Java.util.Arrays.sort and (indirectly) by Java.util.Collections.sort have been replace D. The new sort implementation may throw a illegalargumentexception if it detects a comparable that violates the Comparab Le contract. The previous implementation silently ignored such a situation. If the previous behavior is desired, you can use the new system property, Java.util.Arrays.useLegacyMergeSort, to restore Previous mergesort behavior. Nature of incompatibility:behavioralrfe:6804124

The description means that the sorting algorithm in the Java.util.Arrays.sort (also this method called by Java.util.Collections.sort) method has been replaced in JDK7. If the constraint of the comparison is violated the new sorting algorithm may throw llegalargumentexception exceptions. The implementation in JDK6 ignores this. So what are the constraints of comparison? Look here, roughly as follows:

    • SGN (compare (x, y)) = =-SGN (compare (y, x))
    • ((compare (x, y) >0) && (compare (Y, z) >0)) implies compare (x, z) >0
    • Compare (x, y) ==0 implies that sgn (compare (x, z)) ==sgn (compare (Y, z)) as all Z
And then look back at the implementation of the problem:
return x > Y? 1:-1;
When x = = y, sgn (compare (x, y)) = -1,-sgn (compare (y, x)) = 1, which violates SGN (compare (x, y) = =-sgn (compare (y, x)) constraints, so in JDK7 throws the title of this article Abnormal. Conclusion so now whether the coffin can be finalized, according to the above analysis, using this comparison method (return x > y? 1:-1;), the exception to this article's title is thrown whenever the collection or array has the same element. In fact, in what case throws an exception, also depends on the implementation of JDK7 bottom ranking algorithm, that is, the famous timsort. Later articles will analyze Timsort. This article gives a case that will cause the exception to be studied together, as follows:
Integer[] Array = {0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 , 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 2, 1, 0, 0, 0, 2, 30, 0, 3};
(end) This article from: Gao | Coder, the original address: http://blog.csdn.net/ghsau/article/details/42012365, reprint please specify.

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.