Comparber接口和Comparator接口比较俩个数大小

一、作用

1 、java.lang.Comparable(优先考虑它)(自然排序接口,自然比较接口)
3、java.util.Comparator(作为上面接口的补充,备胎)(定制排序接口,定制比较接口)
上面两个接口有点像孪生兄弟,经常一起出现,所以要注意区分。这两个接口的作用:当Java的对象要比较大小时,要排序时,就要实现他们两个接口之一。

二、 举例说明

例:我们要实现学生对象的排序或比较大小
(1)要让学生类Student实现java.lang.Comparable,并且重写int compareTo(Object obj)。
默认按照年龄比较大小

public class Student implements Comparable{

    private String name;
    private int age;
    private int score;
    private double weight;

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

    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 int getScore() {
        return score;
    }

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

    public double getWeight() {
        return weight;
    }

    public void setWeight(double weight) {
        this.weight = weight;
    }

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

    /*
        这里为什么不能把compareTo方法的形参,直接写成Student呢?
        因为在父接口Comparable中 这个抽象方法的声明就是
        public int compareTo(Object o);
        实现类重写的话,不能修改形参的类型。
         */
    @Override
    public int compareTo(Object o) {
        //假设默认是按照年龄比较大小
        Student other = (Student) o;//向下转型,为了调用o对象中的age
        return this.age - other.age;
        /*
        这个方法返回值是int,重写方法时,返回值类型是int的话不能变。
        int值有三种情况,规定:
        this对象 大于 o对象,返回正整数
        this对象 小于 o对象,返回负整数
        this对象 等于 o对象,返回0
         */
    }
}


(2)、可以为学生类的对象比较成绩但是声明一个类StudentScoreComparator,实现java.util.Comparator接口,重写接口的int compare(Object o1, Object o2)

public class StudentScoreComparator  implements Comparator {
    /*
    compare方法目前的形参只能是Object,因为重写时形参列表必须一致
     */
    @Override
    public int compare(Object o1, Object o2) {
        //比较两个学生对象的成绩
        Student s1 = (Student) o1;
        Student s2 = (Student) o2;
        return s1.getScore() - s2.getScore();
         /*
        这个方法返回值是int,重写方法时,返回值类型是int的话不能变。
        int值有三种情况,规定:
        o1对象 大于 o2对象,返回正整数
        o1对象  小于 o2对象,返回负整数
        o1对象  等于 o2对象,返回0
         */
    }
}

(3)测试类

public class TestCompare {
    public static void main(String[] args) {
        Student s1 = new Student("张三",24,89, 75.5);
        Student s2 = new Student("李四",24,85,62.0);

        //比较s1和s2的大小,默认按照年龄比较大小的
        System.out.println("按照年龄比较:");
        if(s1.compareTo(s2)>0){
            System.out.println("s1 > s2");
        }else if(s1.compareTo(s2)<0){
            System.out.println("s1 < s2");
        }else{
            System.out.println("s1 = s2");
        }

        System.out.println("按照成绩比较:");
        //需要调用StudentScoreComparator类的非静态compare方法比较两个学生对象的成绩
        StudentScoreComparator sc = new StudentScoreComparator();
        if(sc.compare(s1,s2) > 0){
            System.out.println("s1的成绩 高于 s2的成绩");
        }else if(sc.compare(s1,s2) < 0){
            System.out.println("s1的成绩 低于 s2的成绩");
        }else{
            System.out.println("s1的成绩 等于 s2的成绩");
        }
 }
}

猜你喜欢

转载自blog.csdn.net/wangjiziji123/article/details/121413532