JDK7版本运行抛出异常:Comparison method violates its general contract

发布于 2021-11-29 20:30:45 字数 3413 浏览 773 评论 6

最近在学Java,后续尝试用Java在云服务器上做个项目

不过在做一个年龄排序题目的时候,JDK7版本运行抛出异常:Comparison method violates its general contract。

下面是代码:

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;


public class SortTest {
    static class Student {
        //姓名
        String name;
        //年龄
        int    age;
        //性别
        String sex;


        public Student(String name, int age, String sex) {
            super();
            this.name = name;
            this.age = age;
            this.sex = sex;
        }


        /**
         * @return the name
         */
        public String getName() {
            return name;
        }


        /**
         * @return the age
         */
        public int getAge() {
            return age;
        }


        /**
         * @return the sex
         */
        public String getSex() {
            return sex;
        }


        @Override
        public String toString() {
            return "name:" + name + ",age:" + age + ",sex:" + sex;
        }


    }


    /**
     * @param args
     */
    public static void main(String[] args) {


        System.setProperty("java.util.Arrays.useLegacyMergeSort", "false");


        Student stu1 = new Student("张三", 11, "男");
        Student stu2 = new Student("李四", 12, "男");
        Student stu3 = new Student("王五", 13, "女");
        Student stu4 = new Student("刘六", 12, "女");
        Student stu5 = new Student("张三", 11, "男");
        Student stu14 = new Student("张三", 11, "男");
        Student stu6 = new Student("张三", 11, "男");
        Student stu7 = new Student("张三", 11, "男");
        Student stu8 = new Student("张三", 11, "男");
        Student stu9 = new Student("张三", 12, "男");
        Student stu10 = new Student("张三", 11, "男");
        Student stu11 = new Student("张三", 11, "男");
        Student stu12 = new Student("张三", 15, "男");
        Student stu13 = new Student("张三", 11, "男");
        List<Student> stuList = Arrays.asList(stu1, stu2, stu3, stu4, stu5, stu6, stu7, stu8, stu9, stu10, stu11,
                stu12, stu13, stu14, stu5, stu1, stu2, stu3, stu4, stu1, stu2, stu3, stu4, stu5, stu6, stu7, stu8,
                stu9, stu10, stu11, stu12, stu13, stu14, stu5, stu1, stu2, stu3, stu4, stu1, stu2, stu3, stu4, stu5,
                stu6, stu7, stu8, stu9, stu10, stu11, stu12, stu13, stu14, stu5, stu1, stu2, stu3, stu4, stu1, stu2,
                stu3, stu4, stu5, stu6, stu7, stu8, stu9, stu10, stu11, stu12, stu13, stu14, stu5, stu1, stu2, stu3,
                stu4, stu1, stu2, stu3, stu4, stu5, stu6, stu7, stu8, stu9, stu10, stu11, stu12, stu13, stu14, stu5,
                stu1, stu2, stu3, stu4);
        Collections.sort(stuList, new Comparator<Student>() {


            @Override
            public int compare(Student o1, Student o2) {
                return o1.getAge() > o2.getAge() ? 1 : (o1.getAge() == o2.getAge() ? 0 : -1);
            }
        });
        for (Student student : stuList) {
            System.out.println(student);
        }
        List<Integer> list = Arrays.asList(2, 1, 1, 3, 2);
        Collections.sort(list, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1 > o2 ? 1 : -1;
            }
        });


    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

左岸枫 2021-12-03 04:38:30

需要看你的云服务器上的jdk版本

离不开的别离 2021-12-03 03:08:19

这个我最近也遇到过。这个jdk引起的,原因时jdk1.7修改了排序算法,而且这个问题在1.7之前是不会报错。参考地址:http://stackoverflow.com/questions/10234038/compare-method-throw-exception-comparison-method-violates-its-general-contract

海之角 2021-12-02 20:59:24

http://stackoverflow.com/questions/7849539/comparison-method-violates-its-general-contract-java-7-only

静谧 2021-12-02 20:47:48

System.setProperty("java.util.Arrays.useLegacyMergeSort", "true");  

醉生梦死 2021-12-02 10:13:51

看jdk版本,我本地测试的jdk没有报错

java version "1.7.0_65"
Java(TM) SE Runtime Environment (build 1.7.0_65-b19)
Java HotSpot(TM) Client VM (build 24.65-b04, mixed mode, sharing)

看看你的云服务器上的jdk版本

倚栏听风 2021-11-30 18:43:12

 return o1 > o2 ?1 : -1;

没有判断相等的情况

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文