Comparison method violates its general contract!

package test_jdk;

import java.util.Arrays;
import java.util.List;

public class Main {
	 public static void main(String[] args) {

	        sort(1, 1, 1, 1, 1, 2, 1, 1, 1);
	        sort(3, 2, 3, 2, 1, 31);
	        sort(2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3);

	        // exception
	        sort(1, 2, 3, 2, 2, 3, 2, 3, 2, 2, 3, 2, 3, 3, 2, 2, 2, 2, 2, 2, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
	                1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1);

	    }

	    private static void sort(Integer... ints) {
	        List<Integer> list = Arrays.asList(ints);
	        list.sort((o1, o2) -> {
	        	
	            if (o1 < o2) {
	                return -1;
	            } else {
	                return 1;
	            }
	           
	        	/*
	        	
	        	if (o1 < o2) {
	                return -1;
	            }else if(o1<02) {
	                return 1;
	            } else {
	            	return 0;
	            }
	            */
	        });
	        System.out.println(list);
	    }
}

参考:https://www.jianshu.com/p/4e568ef541ae

https://programtalk.com/java/comparison-method-violates-general-contract/

文/程忠 浏览次数:0次   2021-04-14 13:52:39

相关阅读


评论: