Comparable和Comparator的使用

题目大概就是这样:一组数据按照分数score降序排列,如果分数相同则按照年龄生序排列。废话不多说直接上代码

o1-o2:如果返回的是负数则顺序不变,返回的是正数则交换o1,o2的位置

也就是说o1 - o2是升序,o2-o1是降序 

Comparable:(这里我用的是HashSet)

  public static void main(String[] args) {
        TreeSet<Student> data = new TreeSet<>();
        Student s1 = new Student("贾宝玉",14,88.5);
        Student s2 = new Student("林黛玉",13,90.5);
        Student s3 = new Student("史湘玉",13,85);
        Student s4 = new Student("薛宝钗",15,91);
        data.add(s1);
        data.add(s2);
        data.add(s3);
        data.add(s4);
        for (Student s:data){
            System.out.println(s);
        }
    }

    static class Student implements Comparable<Student> {//想要使用自定义方法存到HashTree中,需要继承Comparable,实现compareTo方法定义排序方式
        private String name;
        private int age;
        private  double score;

        @Override
        public int compareTo(Student o) {
            //this与o比较 ,返回的数据如果是负数则this小,0相等,整数this大
            if (this.score > o.score) {//如果存储的分数比下一个小则返回-1
                return -1;//-1表示小
            } else if (this.score == o.score) {
                if (this.age > o.age){
                    return  1;
                }else  if (this.age <= o.age){
                    return -1;
                }
            }
            return 1;
        }

        public Student(String name, int age, double score) {
            this.name = name;
            this.age = age;
            this.score = score;
        }

        public String toString() {
            return "Student{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    ", score=" + score +
                    '}';
        }
    }

运行结果:

Comparator:(这里我用的是ArrayList)

public static void main(String[] args) {
        ArrayList<Student> list = new ArrayList<Student>();
        Student s1 = new Student(88.5,"贾宝玉",14);
        Student s2 = new Student(90.5,"林黛玉",13);
        Student s3 = new Student(85,"史湘玉",13);
        Student s4 = new Student(91,"薛宝钗",15);
        list.add(s1);
        list.add(s2);
        list.add(s3);
        list.add(s4);
        //比较器构建
        Comparator<Student> studentComparator = new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                if((int)o2.score - (int) o1.score == 0){
                    return  o1.age - o2.age;
                }
                return (int)o2.score - (int) o1.score;//s如果o2- o1大于0 则o2在o1的左边 上边 中
            }
        };
        //使用比较器排序
        Collections.sort(list,studentComparator);//结合用Collections,数组用Arrays

        //打印输出
        for (Student s:list){
            System.out.println(s);
        }
    }
    static class  Student {
        private String name;
        private int age;
        private  double score;



        public Student(double score, String name,int age) {
            this.score = score;
            this.name = name;
            this.age = age;
        }

        public String toString() {
            return "Student{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    ", score=" + score +
                    '}';
        }
    }

运行结果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值