On the Illegalargumentexception:comparison method violates it general contract of JDK7

Source: Internet
Author: User
Tags comparable java se
on the Illegalargumentexception:comparison method violates it general contract of JDK7

One of the questions I saw today when I looked at the community quiz was about the implementation of Compartor in the Ali Development Manual, which is recommended to achieve symmetry, transitivity, and reflexivity. The answer mentions the problems that arise in the JDK7 if such a property is not satisfied.

This is when I know the original JDK7 and above version, the function used to compare must satisfy the above nature. reason

The reason is that the sorting algorithm is redesigned in JDK7, from the general merging order to the Timsort, this algorithm needs to use the comparison function which satisfies the symmetry and the transitivity. Otherwise, the sorting algorithm does not work correctly, and the IllegalArgumentException is thrown: Comparison method violates the exception of its general contract. In the previous JDK version, only the less satisfying comparison functions were ignored and no feedback was available.
Refer to the relevant content:

Area : api:utilities
Synopsis : Updated sort behavior for Arrays and collections May throw a illegalargumentexception
Description : The sorting algorithm used by JAVA.UTIL.ARRAYS.S ORT and (indirectly) by Java.util.Collections.sort has been replaced. The new sort implementation may throw a illegalargumentexception if it detects a comparable that violates the comparable Contract. The previous implementation silently ignored such a situation.
If The previous behavior is desired, your can use the new system property, Java.util.Arrays.useLegacyMergeSort, to Res Tore previous mergesort behavior.
nature of incompatibility : Behavioral
RFE : 6804124

The description is that the algorithm used by Java.util.Arrays.sort and Java.util.Collections.sort is replaced, and the new algorithm finds that a comparable object violates the contract of comparison (i.e. three properties). It is possible to throw a illegalargumentexception, and in the previous version of the implementation of the illegal comparison of the case, just silently ignored.
If you want to use a previous version of the implementation, you can use the new System properties: Java.util.Arrays.useLegacyMergeSort to restore the previous version of the merge sort behavior. What if you must use a comparison function that does not satisfy the nature?

There is also a way to JDK7 the original sorting algorithm, and when we configure Java.util.Arrays.useLegacyMergeSort to be true, we can also use a comparison function that is not satisfying on the JDK7.
The specific methods are:
1. If it is application, or IDE, add parameters when you want to start-djava.util.arrays.uselegacymergesort=true
2. If it is a server or other running environment, you can add it in the configuration file: java_opts= "$JAVA _opts-djava.util.arrays.uselegacymergesort=true"
3. Run

System.setproperty ("Java.util.Arrays.useLegacyMergeSort", "true");

However, it is said that the 3rd method does not necessarily work because the property is static and cannot be changed after the JVM has loaded the arrays.
For more detailed description, please see the reference source at the end of the article 2 and source 3

Finally, it is best to implement a comparison function that satisfies the symmetry, transitivity and reflexivity when implementing the comparison function. Reference Sources

Finally, for a detailed explanation of Timsort, see Reference Source 1, which also provides links to more information about Timsort to learn more about
How to configure Java.util.Arrays.useLegacyMergeSort on JDK7. See reference source 2, source 3
for compatibility issues with JDK7, see Source 4, which is the relevant content from this.
1. The comparison method violates it general contract exception
2, illustrated by JDK7. "Comparison method violates it general contract!"-Timsort and GridLayout
3. Comparison method violates it general contract! Java 7 only
4. Java SE 7 and JDK 7 compatibility

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.