1、接口Comparable的特点:
和java内置类Integer和String等类一样,一个类实现了Comparable接口重写了CompareTo方法,将这个类实例放入List中,就可以使用Collections中的sort方法进行排序。
例子:
有一个学生类包含姓名和成绩属性,将几个学生放入容器studentlist中,需要按规则对容器内的学生排序:按照名字首字母排序,如果名字相同,按照份数从高到低排序。
package Compara;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
//创建学生类,实现Comparable接口
public class Student implements Comparable<Student> {
//定义学生类的名字name和分数mark属性
public String name;
public int mark;
//定义构造方法
public Student(String name, int mark) {
super();
this.name = name;
this.mark = mark;
}
//此学生类由于实现了Comparable接口,必须重写comparaTo方法。
@Override
public int compareTo(Student anotherStudent) {
if (this.name.compareTo(anotherStudent.name) > 0) {
return 1;
} else if (this.name.compareTo(anotherStudent.name) == 0) {
if (this.mark - anotherStudent.mark > 0) {
return 1;
} else if (this.mark - anotherStudent.mark == 0) {
return 0;
} else {
return -1;
}
} else {
return -1;
}
}
public static void main(String[] args) {
//new出来四个学生对象
Student stu1=new Student("Liming",568);
Student stu2=new Student("Liming",580);
Student stu3=new Student("Zhenh",480);
Student stu4=new Student("Pengzu",668);
//new一个studentlist,将四个学生扔进去。
List<Student> studentlist=new ArrayList<Student>();
studentlist.add(stu1);
studentlist.add(stu2);
studentlist.add(stu3);
studentlist.add(stu4);
//条用Collections下的sort方法进行排序。
Collections.sort(studentlist);
//打印出排序后的情况
for(Student stu:students){
System.out.println(stu.name+"=>"+stu.mark);
}
}
}
输出结果:
Liming=>568
Liming=>580
Pengzu=>668
Zhenh=>480
2、Comparator接口的特点:
还是上面学生的例子,要使用Comparator(比较器)来实现学生排序,需要在自己定义一个StudentComparator类,实现Comparator接口和重载compare方法,然后使用Collections.sort(studentlist,new StudentComparator())来排序。
在学生类外部自定义一个StudentComparator类,实现Comparator接口:
package Compara;
import java.util.Comparator;
//自定义一个StudentComparator类,实现Comparator接口,重载compare方法
public class StudentComparator implements Comparator<Student> {
@Override
public int compare(Student o1, Student o2) {
if(o1.name.compareTo(o2.name)>0){
return 1;
}else if(o1.name.compareTo(o2.name)==0){
if(o1.mark-o2.mark>0){
return 1;
}else if(o1.mark-o2.mark==0){
return 0;
}else{
return -1;
}
}else{
return -1;
}
}
学生类,此时需要使用Collections.sort(studentlist,new StudentComparator())将自定义的比较器传进去。
ackage Compara;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Student {
//定义学生类的名字name和分数mark属性
public String name;
public int mark;
//定义构造方法
public Student(String name, int mark) {
super();
this.name = name;
this.mark = mark;
}
public static void main(String[] args) {
//new出来四个学生对象
Student stu1=new Student("Liming",568);
Student stu2=new Student("Liming",580);
Student stu3=new Student("Zhenh",480);
Student stu4=new Student("Pengzu",668);
//new一个list,将四个学生扔进去。
List<Student> studentlist=new ArrayList<Student>();
studentlist.add(stu1);
studentlist.add(stu2);
studentlist.add(stu3);
studentlist.add(stu4);
//条用Collections下的sort方法进行排序。
Collections.sort(studentlist,new StudentComparator());
//打印出排序后的情况
for(Student stu:studentlist){
System.out.println(stu.name+"=>"+stu.mark);
}
}
}
可以看出,假如我们的学生类是一个java的内置类(如Integer和String等类),我们就可以不动源码的情况下,实现排序功能。
网友评论