comparable接口 和 comparator接口实例

Student类实现Comparable接口 

class Student implements Comparable<Student>{

    private String name;
    private int age;
    private float score;

    public Student() {
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public float getScore() {
        return score;
    }

    public void setScore(float score) {
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}';
    }
    //成绩降序,年龄升序
    @Override
    public int compareTo(Student o) {
        int result = 0;
        result = (int) -(this.score - o.score);
        if(result == 0){
            result = this.age - o.age;
        }
        return result;
    }


}

 Comparable接口 测试

public class ComparableTest {
    public static void main(String[] args) {
        List<Student> students = new ArrayList<>();
        students.add(new Student("liusan",20,90.0f));
        students.add(new Student("lisi",22,90.0f));
        students.add(new Student("wangwu",20,99.0f));
        students.add(new Student("sunliu",22,100.0f));
        Collections.sort(students);
        for(Student s : students){
            System.out.println(s);
        }
    }
}

StudentComparator 实现 Comparator接口

class StudentComparator implements Comparator<Student> {

    //成绩降序,年龄升序
    @Override
    public int compare(Student o1, Student o2) {
        int result = 0;
        result = (int) -(o1.getScore() - o2.getScore());
        if(result == 0){
            result = o1.getAge() - o2.getAge();
        }
        return result;
    }

}

Comparator测试 

public class ComparatorTest {
    public static void main(String[] args) {
        List<Student> students = new ArrayList<>();
        students.add(new Student("liusan",20,90.0f));
        students.add(new Student("lisi",22,90.0f));
        students.add(new Student("wangwu",20,99.0f));
        students.add(new Student("sunliu",22,100.0f));
        Collections.sort(students,new StudentComparator());
        for(Student s : students){
            System.out.println(s);
        }
    }
}

结果 

Student{name='sunliu', age=22, score=100.0}
Student{name='wangwu', age=20, score=99.0}
Student{name='liusan', age=20, score=90.0}
Student{name='lisi', age=22, score=90.0}

Process finished with exit code 0

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值