Collections.sort will raise IllegalArgumentException.

Description:

1) Reflexivity: the comparison result of X and Y is opposite to that of Y and x.

2) Transitivity: x>y,y>z, then x>z.

3) Symmetry: x=y, then the comparison result of X and z is the same as that of y and z. (also called reversible comparison)

Counterexample: In the following example, equality is not handled. Exceptions may occur in actual use: Collections.sort(list, new Comparator() {@override public int compare(Integer o1, Integer o2) {return o1 > o2? 1:1; // Wrong way}}); The solution

Let’s talk about how to solve it. There are two ways to solve it.

Collections.sort(list, new Comparator() {@override public int compare(Integer o1, Integer o2) { // return o1 > o2 ? 1:1; return o1.compareTo(o2); // The correct way}});

Collections.sort(list, new Comparator() {@override public int compare(Integer o1, Integer o2) { if(o1 > o2) { return 1; } else if (o1< o2){ return -1; // if a>b, b>c, then a>c must be true. If o2==null, return 0; if o2==null, return 0; }}}); (to really solve the problem, and not like a lot of online posts add a is equal to the conditions described in can, this is because the inverse problems, you should solve the problem from the source, is according to your own code logic judgment whether must comply with the above character of reflexivity, later I will find out the special scenario)

Don’t modify the code

So here’s the question. Why does the above code run fine in JDK6 but throw exceptions in JDK7? If you want to use the JDK6 sort algorithm, you can add the following parameters to the startup parameters of the JVM: – Djava. Util. Arrays. UseLegacyMergeSort = true will still use JDK6 has sorting algorithm, in the case of can’t modify the code to solve the problem of incompatibility. (This may be an operational problem, I do not recommend using this bug. In my opinion, this bug will cause you to be unable to use the new features in JDK1.7, which will have an unknown impact on the later upgrade.) Return x > y? Return x > y? 1:1) Throws an exception in the title of this article whenever there are identical elements in the collection or array. In fact, it is up to the implementation of JDK7’s underlying sorting algorithm, also known as TimSort, to decide when to throw an exception. TimSort will be examined later in this article. This paper gives a Case that will cause the exception, so that people can study 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, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 2, 1, 0, 0, 0, 2, 0, 30, 3};

Further discussion: The above is my conclusion after a day of checking, but it still fails to solve the actual problem in my project. If the scene of == is added, the problem is still not solved, which is basically caused by the wrong logic rewritten in Compare, so we need to re-check whether there is a problem in our code. However, this kind of code is generally hidden deeply. The following example is more similar to my actual situation. Can you see why this error is also reported? (This kind of scene is hidden so deeply that it can’t be discovered without repeated thinking.)

Foreigner explanation: Comparison methods are not transitive. For example if A==B and B==C, then A must be equal to C. Now look at the case of this example: suppose three objects A, B, and C. Suppose the inclusion case looks like this: childMap.containsKey(A.getID()) returns true childMap.containsKey(B.getID()) returns false Childmap.containskey (c.getid ()) returns true if A does not satisfy the if condition when comparing A with B. If C does not satisfy the if condition when comparing A with B, it returns 0. That means that B is equal to C. Suppose that the comparison between A and C is likely to return either 1 or -1, which results in A! =C. This violates the delivery rule. So you can’t just return 0 inside the else, so it depends.

That was the end of my problem. Refer to the blog: blog.csdn.net/TomCosin/ar… www.cnblogs.com/firstdream/… www.jianshu.com/p/aebaa787c…