在之前的博客中,我们简单谈过如何利用Compable接口来进行排序,这次我们具体来看下Compable和Comparator的区别及用法。
首先我们还是来看看上次我们看的那个例子
public class Student implements Comparable {
private String name;
private int age;
private double score;
public Student(){}
public Student(String name, int age, double score) {
this.name = name;
this.age = age;
this.score = score;
}
@Override
public int compareTo(Object o) {
Student student = (Student) o;
if(this.score > student.score){
return 1;
}else if(this.score < student.score){
return -1;
}else
return 0;
}
public void print(){
System.out.println("姓名:"+name+"\t"+"年龄:"+age+"\t"+"成绩:"+score);
}
}
public class Client {
public static void main(String[] args) {
Student[] students = {
new Student("张三",20,99),
new Student("李四",19,67),
new Student("王五",22,81)
};
Arrays.sort(students);
for(Student student : students){
student.print();
}
}
}
运行结果:
姓名:李四 年龄:19 成绩:67.0
姓名:王五 年龄:22 成绩:81.0
姓名:张三 年龄:20 成绩:99.0
我们可以看到结果按照成绩从小到大的依次排序打印出来,但是我们现在想要从大到小排序又该怎么做呢?我们最容易想到的就是把Student类中的CompareTo方法改一下就好,简单。没错,这次的确就可以实现从大到小排序了,但是依照我们以前说过的设计原则,对修改关闭,对扩展开放,就不可以这么做,退一步说就算可以,那要是我们在这儿需要从大到小,在别的地方又要从小到大那又该怎么样呢?这时Comparator就帮助我们完成这个需求了。
//Arrays.sort(students);
Arrays.sort(students, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o2.compareTo(o1);
}
});
运行结果:
姓名:张三 年龄:20 成绩:99.0
姓名:王五 年龄:22 成绩:81.0
姓名:李四 年龄:19 成绩:67.0
该方法对集合同样适用,如下:
public class Client {
public static void main(String[] args) {
List<Student> list = Arrays.asList(
new Student("张三",20,99),
new Student("李四",19,67),
new Student("王五",22,81)
);
Collections.sort(list);
for(Student student : list){
student.print();
}
}
}
运行结果:
姓名:李四 年龄:19 成绩:67.0
姓名:王五 年龄:22 成绩:81.0
姓名:张三 年龄:20 成绩:99.0
//Collections.sort(list);
Collections.sort(list, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o2.compareTo(o1);
}
});
运行结果:
姓名:张三 年龄:20 成绩:99.0
姓名:王五 年龄:22 成绩:81.0
姓名:李四 年龄:19 成绩:67.0
我们再思考一个问题,要是我们再Student内并没有实现Compable这个接口,是不是就不可以进行排序了呢?
public class Student {
private String name;
private int age;
private double score;
public Student(){}
public Student(String name, int age, double score) {
this.name = name;
this.age = age;
this.score = score;
}
public void print(){
System.out.println("姓名:"+name+"\t"+"年龄:"+age+"\t"+"成绩:"+score);
}
}
public class Client {
public static void main(String[] args) {
List<Student> list = Arrays.asList(
new Student("张三",20,99),
new Student("李四",19,67),
new Student("王五",22,81)
);
Collections.sort(list); //错误
//sort (java.util.List<T>) in Collections cannot be applied to (java.util.List<Student>)
for(Student student : list){
student.print();
}
}
}
但我们去除掉Student中的Compable接口后,场景类Client中Collections.sort(list)其实已经报错了,编译都无法通过。
如果想要进行排序,同样,我们也需要用到Comparator,另外我们还需在Student类中加入Getter方法。
//Collections.sort(list);
Collections.sort(list, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return (int) (o1.getScore()-o2.getScore());
}
});
运行结果:
姓名:李四 年龄:19 成绩:67.0
姓名:王五 年龄:22 成绩:81.0
姓名:张三 年龄:20 成绩:99.0