comparable和comparator

Comparable

Comparable可以认为是一个内部比较器,实现了Comparable接口的类有一个特点,就是这些类是可以和自己比较的,在compareTo方法中指定具体的比较方法。

compareTo方法的返回值是int,有三种情况:

1、比较者大于被比较者(也就是compareTo方法里面的对象),那么返回正整数

2、比较者等于被比较者,那么返回0

3、比较者小于被比较者,那么返回负整数

  举例:定义Student类,实现Comparable接口,重写compareTo方法,比较两者的age。

public class Student implements Comparable<Student>{

    private String name;
    private String age;

    public Student() {
    }

    public Student(String name, String age) {

        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

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

    public String getAge() {
        return age;
    }

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

    @Override
    public int compareTo(Student o) {
        return this.age.compareTo(o.age);
    }
}

  

public class Test {
    public static void main(String[] args) {
        Student student1 = new Student("zs","11");
        Student student2 = new Student("ls","12");
        Student student3 = new Student("ww","10");
        System.out.println("age 11 与 age 12 比较====" + student1.compareTo(student2));
        System.out.println("age 11 与 age 10 比较====" + student1.compareTo(student3));
        System.out.println("age 11 与 age 11 比较====" + student1.compareTo(student1));
    }
}

  结果:

age 11 与 age 12 比较====-1
age 11 与 age 10 比较====1
age 11 与 age 11 比较====0

Comparator

Comparator可以认为是是一个外部比较器,个人认为有两种情况可以使用实现Comparator接口的方式:

1、一个对象不支持自己和自己比较(没有实现Comparable接口),但是又想对两个对象进行比较

2、一个对象实现了Comparable接口,但是开发者认为compareTo方法中的比较方式并不是自己想要的那种比较方式

Comparator接口里面有一个compare方法,方法有两个参数T o1和T o2,是泛型的表示方式,分别表示待比较的两个对象,方法返回值和Comparable接口一样是int,有三种情况:

1、o1大于o2,返回正整数

2、o1等于o2,返回0

3、o1小于o3,返回负整数

  举例:自定义一个外部比较器StudentComparator,实现Comparator接口,重写compare方法,比较两者的age。

public class StudentComparator implements Comparator<Student>{

    @Override
    public int compare(Student o1, Student o2) {
        return o1.getAge().compareTo(o2.getAge());
    }
}
public class Test {
    public static void main(String[] args) {
        Student student1 = new Student("zs","11");
        Student student2 = new Student("ls","12");
        Student student3 = new Student("ww","10");
        StudentComparator studentComparator = new StudentComparator();
        int compare1 = studentComparator.compare(student1, student2);
        int compare2 = studentComparator.compare(student1, student3);
        int compare3 = studentComparator.compare(student1, student1);
        System.out.println("age 11 与 age 12 比较======" + compare1);
        System.out.println("age 11 与 age 10 比较======" + compare2);
        System.out.println("age 11 与 age 11 比较======" + compare3);
    }
}

  结果:

age 11 与 age 12 比较======-1
age 11 与 age 10 比较======1
age 11 与 age 11 比较======0

总结

总结一下,两种比较器Comparable和Comparator,后者相比前者有如下优点:

1、如果实现类没有实现Comparable接口,又想对两个类进行比较(或者实现类实现了Comparable接口,但是对compareTo方法内的比较算法不满意),那么可以实现Comparator接口,自定义一个比较器,写比较算法

2、实现Comparable接口的方式比实现Comparator接口的耦合性要强一些,如果要修改比较算法,要修改Comparable接口的实现类,而实现Comparator的类是在外部进行比较的,不需要对实现类有任何修改。从这个角度说,其实有些不太好,尤其在我们将实现类的.class文件打成一个.jar文件提供给开发者使用的时候。实际上实现Comparator接口的方式后面会写到就是一种典型的策略模式

当然,这不是鼓励用Comparator,意思是开发者还是要在具体场景下选择最合适的那种比较器而已。

参考资料:

https://www.cnblogs.com/xrq730/p/4850140.html

posted @ 2019-01-29 11:39  吹灭读书灯  阅读(169)  评论(0编辑  收藏  举报