【Java】:Comparable接口和Comparator接口

对象数组排序

通过年龄比较


Comparable接口

  • 此处运用到 comparable 接口,其内置方法为 comparaTo
  • 返回类型为整形
public interface Comparable<T> {
    public int compareTo(T o);
}

代码

class Student implements Comparable<Student> { //实现了Comparable接口的Student类
    public String name;
    public int age;

    public Student(String name, int age) { //构造方法
        this.name = name;
        this.age = age;
    }

    @Override  
    //重写Comparable接口内的comparaTo方法
    public int compareTo(Student o) {  
        return this.age - o.age;  //即调用方法的对象 - 参数对象
    }

    public static void main(String[] args) {
        Student student1 = new Student("John Smith", 10);
        Student student2 = new Student("Don Michael", 20);

        if(student1.compareTo(student2) > 0){   
        	//若student1 - student2 > 0,则打印:
            System.out.println("John Smith > Don Michael");
        } else if (student1.compareTo(student2) < 0) {   
        	//若student1 - student2 < 0,则打印:
            System.out.println("John Smith < Don Michael");
        }else{  
        	//若student1 - student2 = 0,则打印:
            System.out.println("John Smith = Don Michael");
        }
    }
}

//运行结果为:John Smith < Don Michael

通过姓名比较

字符串大小比较

  • 字符串的大小比较是指按照字典次序对单个字符或字符串进行比较大小的操作,一般都是以ASCII码值的大小作为字符比较的标准。

  • 比如 abcade 比较大小

    • 因为双方第一个字母相等,都是 a,所以比较对象均向后移一位
    • 由于 b < d,所以 abc < ade

代码

class Student implements Comparable<Student> {
    public String name;
    public int age;

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

    @Override
    public int compareTo(Student o) {
        if(this.name.compareTo(o.name) > 0){  //即调用方法的对象 - 参数对象
            return 1;
        } else if (this.name.compareTo(o.name) < 0) {
            return -1;
        }else {
            return 0;
        }
    }

    public static void main(String[] args) {
        Student student1 = new Student("John Smith", 10);
        Student student2 = new Student("Don Michael", 20);

        if(student1.compareTo(student2) > 0){
        	//若student1 - student2 > 0,则打印
            System.out.println("John Smith > Don Michael");
        } else if (student1.compareTo(student2) < 0) {
            //若student1 - student2 < 0,则打印
            System.out.println("John Smith < Don Michael");
        }else{
        	//若student1 - student2 = 0,则打印
            System.out.println("John Smith = Don Michael");
        }
    }
}

对比

  • 对两个类的重写方法进行对比,可以发现问题:
    • Comparable 这个接口具有局限性,一但这个类写死了,后期就无法进行修改
    • 可以理解为,写死的就是一个默认的比较方式
    • 由此,我们可以使用一个新的接口 Comparator

比较器——Comparator接口

  • Comparator接口返回类型也为整形
  • 其内部有很多内置方法
public interface Comparator<T> { //三处 T 必须都得一样
	int compare(T o1, T o2);
	//还有很多其他方法...
}
年龄比较器
import java.util.Comparator;

class Student {
    public String name;
    public int age;

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

public class AgeComparator implements Comparator<Student> {

    @Override
    public int compare(Student o1, Student o2) {
        return o1.age - o2.age;  
    }

    public static void main(String[] args) {
        Student student1 = new Student("John Smith", 18);
        Student student2 = new Student("Don Michael", 28);

        AgeComparator ageComparator = new AgeComparator();
        if(ageComparator.compare(student1, student2) < 0) {
        	//若student1.age < student2.age,则打印:
            System.out.println("John Smith < Don Michael");
        }else if(ageComparator.compare(student1, student2) > 0) {
        	//若student1.age > student2.age,则打印:
            System.out.println("John Smith > Don Michael");
        }else {
        	//若student1.age == student2.age,则打印:
            System.out.println("John Smith = Don Michael");
        }
    }
}
姓名比较器
import java.util.Comparator;

class Student {
    String name;
    int age;

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

public class AgeComparator implements Comparator<Student> {

    @Override
    public int compare(Student o1, Student o2) {
        return o1.name.compareTo(o2.name);    
    }

    public static void main(String[] args) {
        Student student1 = new Student("John Smith", 23);
        Student student2 = new Student("Don Michael", 13);

        if(ageComparator.compare(student1, student2) == 0){
        	//若student1.name == student2.name,则打印:
            System.out.println("John Smith = Don Michael");
        }else if(ageComparator.compare(student1, student2) < 0){
        	//若student1.age < student2.age,则打印:
            System.out.println("John Smith < Don Michael");
        }else {
        	//若student1.age > student2.age,则打印:
            System.out.println("John Smith > Don Michael");
        }
    }
}

  • 在比较器中,比较的对象可以按照需求动态修改,不是写死的
  • 所以比较器 Comparator 用起来更加方便
  • 9
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

长坡滚雪球

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值