深入浅出Comparable接口和Comparator接口
1.Comparable<T>是一个接口,实现此接口的类都必须重写他的一个方法
int comparaTo(T t)
参数:
o
- 要比较的对象。
ClassCastException
- 如果指定对象的类型不允许它与此对象进行比较。
int compare(T o1, T o2)
-
参数:
-
o1
- 要比较的第一个对象。 -
o2
- 要比较的第二个对象。
返回:
- 根据第一个参数小于、等于或大于第二个参数分别返回负整数、零或正整数。 抛出:
ClassCastException
- 如果参数的类型不允许此 Comparator 对它们进行比较
3.两者具体如何使用,用例子说话。 要求实现五个学生,输入学生姓名,语文成绩,数学成绩,英语成绩,并按总成绩排序
public class Student implements Comparable{
private String stu_name;
private float yuwei_score;
private float math_score;
private float english__score;
private float allscore;
public Student(String stu_name, float yuwei_score, float math_score,
float english__score) {
super();
this.stu_name = stu_name;
this.yuwei_score = yuwei_score;
this.math_score = math_score;
this.english__score = english__score;
this.allscore = yuwei_score+math_score+english__score;
}
public float getAllscore() {return allscore;}
public String getStu_name() {return stu_name;}
public float getYuwei_score() {return yuwei_score;}
public float getMath_score() {return math_score;}
public float getEnglish__score() {return english__score;}
@Override
public int compareTo(Object obj) {
Student stu = (Student)obj;
double score1 = stu.yuwei_score+stu.math_score+stu.math_score;
double score2 = this.yuwei_score+this.math_score+this.math_score;
if(score1<score2)
return -1;
else if(score1>score2)
return 1;
else if(score1==score2)
return this.stu_name.compareTo(stu.stu_name);
return 0;
}
}
public class StartMain {
public static void main(String[] args) {
ArrayList<Student> stulist = new ArrayList<Student>();
stulist.add(new Student("lisi01", 100, 95, 67));
stulist.add(new Student("lisi02", 98, 56, 67));
stulist.add(new Student("lisi03", 80, 85, 77));
stulist.add(new Student("lisi04", 87, 95, 57));
stulist.add(new Student("lisi02", 87, 95, 57));
Collections.sort(stulist);
Iterator<Student> it = stulist.iterator();
while(it.hasNext())
{
Student stu = it.next();
System.out.println(stu.getStu_name()+","+stu.getYuwei_score()
+","+stu.getMath_score()+","+stu.getEnglish__score()
+"===="+stu.getAllscore());
}
}
}
打印结果
lisi01,100.0,95.0,67.0====262.0
lisi02,87.0,95.0,57.0====239.0
lisi04,87.0,95.0,57.0====239.0
lisi03,80.0,85.0,77.0====242.0
lisi02,98.0,56.0,67.0====221.0
以上实现方法是在student类中实现comparable()接口重写ComparaTo()方法,使用Collections类中的 sort方法进行排序。
若我现在的需求改变,要求按照姓名的顺序排序,当然,有的哥们会说改下ComparaTo()方法就可以了啊, 当然,这种方法绝对可以,但是如果在一个比较大的项目中如此处理便要费很大功夫,此时若定义一个 比较器实现Comparator()接口则可以不改变原来代码前提下实现现在的功能。具体做法见下面代码,
public class MyComparator implements Comparator<Student>{
@Override
public int compare(Student stu1, Student stu2) {
return stu1.getStu_name().compareTo(stu2.getStu_name());
}
}
打印结果:
lisi01,100.0,95.0,67.0====262.0
lisi02,98.0,56.0,67.0====221.0
lisi02,87.0,95.0,57.0====239.0
lisi03,80.0,85.0,77.0====242.0
lisi04,87.0,95.0,57.0====239.0
注意上面StartMain中的 Collections.sort(stulist);需要改为Collections.sort(stulist,new MyComparator());
通过上面的例子你知道了两者的区别了吗? 总结:
两种方法各有优劣, 用Comparable 简单, 只要实现Comparable 接口的对象直接就成为一个可以比较的对象, 但是需要修改源代码, 用Comparator 的好处是不需要修改源代码, 而是另外实现一个比较器, 当某个自定义 的对象需要作比较的时候,把比较器和对象一起传递过去就可以比大小了, 并且在Comparator 里面用户可以自 己实现复杂的可以通用的逻辑,使其可以匹配一些比较简单的对象,那样就可以节省很多重复劳动了。