java - JDK7抛出异常:Comparison method violates its ……(急求解决,附赠答案奖励)
天蓬老师
天蓬老师 2017-04-18 10:26:37
0
3
555

最近在学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;
            }
        });

    }
}

急求解答!附赠阿里云免费套餐邀请码一个,能开云服务器数据库,很着急谢谢各位了。

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

全部回覆(3)
洪涛

這個應該是JDK 個別版本引起的
你可以嘗試下:
1>
System.setProperty("java.util.Arrays.useLegacyMergeSort", "true");
2>
public , Integer o2) {

return o1.compareTo(o2);

}

大家讲道理

在JDK7版本以上,Comparator要滿足自反性,傳遞性,對稱性。所以你需將原始碼中的部分:

Collections.sort(list, new Comparator() {

        @Override
        public int compare(Integer o1, Integer o2) {
            return o1 > o2 ? 1 : -1;
        }

改成:

Collections.sort(list, new Comparator() {

        @Override
        public int compare(Integer o1, Integer o2) {
            return o1 > o2 ? 1 : (o1 == o2 ? 0 : -1);
        }
巴扎黑

我用JDK1.7,1.8都運行過,沒有報錯;

 public int compare(Student o1, Student o2) {
                return o1.getAge() > o2.getAge() ? 1 : (o1.getAge() == o2.getAge() ? 0 : -1);
            }

public int compare(Integer o1, Integer o2) {
                return o1 > o2 ? 1 : -1;
            }

這兩個地方是有問題的,JDK無法判斷o1.getAge(),o2.getAge()相等的情況。
而且程式碼也不是這麼寫,應該是直接
return o1.getAge()-o2.getAge()
return o1-o2

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!