Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
456 views
in Technique[技术] by (71.8m points)

java - Comparison method violates its general contract Exception

Below is a block of code that results in exception as indicated,

Code :

Collections.sort( arrayList, new Comparator() 
{
    public int compare( Object o1, Object o2 )
    {
        TypeAdapterSort tas1 = ( TypeAdapterSort ) o1;
        TypeAdapterSort tas2 = ( TypeAdapterSort ) o2;
        if ( tas1.order < tas2.order )
            return -1;
        else
            return 1;
    }
} );

Exception :

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.mergeForceCollapse(TimSort.java:426)
                at java.util.TimSort.sort(TimSort.java:223)
                at java.util.TimSort.sort(TimSort.java:173)
                at java.util.Arrays.sort(Arrays.java:659)
                at java.util.Collections.sort(Collections.java:217)

When I run the same code as a standalone program, the issue never occurs. What is the issue with the comparator here? Is there a way to reproduce the issue in a standalone code?

This issue occurs only on Java 1.7 as there has been change in the implementation on Arrays.sort & Collections.sort. How to change the above code to avoid the issue?. Also, how to reproduce this issue in a standalone code?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to return 0 on equal objects.

        if ( tas1.order < tas2.order ){
            return -1;
        } else if ( tas1.order == tas2.order ){
            return 0;
        } else {
            return 1;
        }

You can read here more


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...