Java中的两种比较器的区别及其延申

参考:https://www.nowcoder.com/tutorial/94/12ae51488ad8480ea282ad676e881d6a


比较器的使用地方:
  • Java对于基本类型的比较时,比的是数值的大小,或字母对应的Ascill码,不会涉及比较器。
  • Java中对于基本类型包装类 及 String类,他们都实现了Comparable接口,用来比较两个对象。
  • 而对于我们自己创建的类Student,如果要对对象进行比较,就得手动实现一个比较策略。
  • 通常当要给TreeMap、TreeSet中放入自定义类Student时,就得实现一个比较策略,否则,你把Student往集合中添加时,就会报异常。
区别:

1、Java中有两种比较器,Comparable、Comparator,用来比较两个对象的大小的。
可以把Comparable理解为内部比较器,而Comparator是外部比较器,写法的区别:

  • Comparable是在实体类的内部去实现Comparable接口的compareTo()方法进行比较。传入一个外部参数进行比对。//比较器是定义在实体类中。

  • 而实现了Comparator接口的方法需要实现compare()方法,对外部传入的两个对象进行比较,从而让外部方法在比较时调用。//比较器的定义没在实体类中,是一个单独的比较器类。
    基本的写法如下:
    内部比较器:Comparable

    class Apple implements Comparable{
    int id;
    double price;

    public Apple(int id, double price) {
    this.id = id;
    this.price = price;
    }
    public int compareTo(Apple o) {
    //return Double.compare(this.getPrice(),o.getPrice());
    if (Math.abs(this.price-o.price)<0.001)
    return 0;
    else
    return (o.price-this.price)>0?1:-1;
    }
    @Override
    public String toString() {
    return “Apple{” +
    “id=” + id +
    “, price=” + price +
    ‘}’;
    }
    }

外部比较器:Comparator:

class AESComparator implements Comparator<Apple>{
    public int compare(Apple o1, Apple o2) {
        if (Math.abs(o1.price-o2.price)<0.001)
            return 0;
        else{
            return (o1.price-o2.price)>0?1:-1;
        }
    }
}

2、两者的另一个区别是:实现Comparator接口代码更加灵活,可以给某个类定义的多个比较器,从而在排序时根据实际场景自由调用,而Comparable接口实现后便不能改动。两种接口的调用方式如下:
//Apple类的升序比较器

class AESComparator implements Comparator<Apple>{
    public int compare(Apple o1, Apple o2) {
        if (Math.abs(o1.price-o2.price)<0.001)
            return 0;
        else{
            return (o1.price-o2.price)>0?1:-1;
        }
    }
}

//Apple类的降序比较器

class  DESComparator implements Comparator<Apple>{
    public int compare(Apple o1, Apple o2) {
        if (Math.abs(o1.price-o2.price)<0.001)
            return 0;
        else {
            return (o1.price-o2.price)>0?-1:1;
        }
    }
}

3、但是要注意的是:不能在比较器中进行double类型的减法操作,因为对于值比较大的double,减法操作容易导致值的溢出,java7对每一种包装类型的比较新增了compare()方法,改造后的代码如下:

class Apple implements Comparable<Apple>{
    int id;
    double price;

    public Apple(int id, double price) {
        this.id = id;
        this.price = price;
    }
    public int compareTo(Apple o) {
        return Double.compare(this.price,o.price);
    }
    
    @Override
    public String toString() {
        return "Apple{" +
                "id=" + id +
                ", price=" + price +
                '}';
    }
}

class AESComparator implements Comparator<Apple>{
    public int compare(Apple o1, Apple o2) {
        return Double.compare(o1.price,o2.price);
    }
}

class  DESComparator implements Comparator<Apple>{
    public int compare(Apple o1, Apple o2) {
        return Double.compare(o2.price,o1.price);
    }
}

TreeMap和TreeSet在排序时如何比较元素?

  • TreeSet要求存放的对象所属的类必须实现Comparable接口,该接口提供了比较元素的compareTo()方法,当插入元素时会回调该方法比较元素的大小。

  • TreeMap要求存放的键 必须实现Comparable接口从而根据键 来对元素进行排序。
    【注】:Java中对于基本类型包装类 及 String类,他们都实现了Comparable接口。

  • Collections工具类的sort方法有两种重载的形式,

    • 第一种要求传入的待排序容器中存放的对象比较实现Comparable接口以实现元素的比较;
    • 第二种不强制性的要求容器中的元素必须可比较,但是要求传入第二个参数,参数是Comparator接口的子类型(需要重写compare方法实现元素的比较),相当于一个临时定义的排序规则,其实就是通过接口注入比较元素大小的算法,也是对回调模式的应用(Java中对函数式编程的支持)。
public class
Student implements Comparable<Student> {
     private String
name;        // 姓名
     private int
age;            // 年龄
     public Student(String name, int age) {
         this.name = name;
         this.age = age;
     }
     @Override
     public String toString() {
         return "Student [name=" +
name + ", age=" + age + "]";
     }
     @Override
     public int compareTo(Student o) {
         return this.age - o.age; // 比较年龄(年龄的升序)
     }
 }

public class Test01 {
	public static void main(String[] args) {
		Set<Student> set = new TreeSet<>(); //(构造器后面的尖括号中不需要写类型)
		set.add(new Student("HaoLUO", 33));
		set.add(new Student("XJWANG", 32));
		set.add(new Student("BruceLEE", 60));
		set.add(new Student("BobYANG", 22));
		for(Student stu : set) {
			System.out.println(stu);
		}
 //      输出结果: 
 //      Student [name=Bob YANG, age=22]
 //      Student [name=XJ WANG, age=32]
 //      Student [name=Hao LUO, age=33]
 //      Student [name=Bruce LEE, age=60]
     }
 }
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值