Java基础之comparator和comparable的区别以及使用
1: 区别:
、Comparable类需要实现此接口,定义在类内,不利于扩展
2 、Comparator更灵活,可以随时自定义比较规则
使用:
2.2、 Collections.sort(List<T> list,Comparator<? super T> c);
Comparable:
class Student implements Comparable<Student>{
private String name;
private int age;
private float score; public Student(String name, int age, float score) {
this.name = name;
this.age = age;
this.score = score;
} public String toString()
{
return name+"\t\t"+age+"\t\t"+score;
}
// 返回正数,零,负数各代表大于,等于,小于。
@Override
public int compareTo(Student o) {
// TODO Auto-generated method stub
if(this.score>o.score)//score是private的,为什么能够直接调用,这是因为在Student类内部
return -1;//由高到底排序
else if(this.score<o.score)
return 1;
else{
if(this.age>o.age)
return 1;//由底到高排序
else if(this.age<o.age)
return -1;
else
return 0;
}
}
} public class ComparableDemo01 { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Student stu[]={new Student("zhangsan",20,90.0f),
new Student("lisi",22,90.0f),
new Student("wangwu",20,99.0f),
new Student("sunliu",22,100.0f)};
java.util.Arrays.sort(stu);
for(Student s:stu)
{
System.out.println(s);
}
}
}
但是在设计类的时候,可能没有考虑到让类实现Comparable接口,那么就需要用到另外的一个比较器接口Comparator。
单一条件排序:
List<Student> stus = new ArrayList<Student>(){
{
add(new Student("张三", 30));
add(new Student("李四", 20));
add(new Student("王五", 60));
}
};
//对users按年龄进行排序
Collections.sort(stus, new Comparator<Student>() { @Override
public int compare(Student s1, Student s2) {
// 升序
//return s1.getAge()-s2.getAge();
return s1.getAge().compareTo(s2.getAge());
// 降序
// return s2.getAge()-s1.getAge();
// return s2.getAge().compareTo(s1.getAge());
}
});
// 输出结果
...
还可以使用lambda表达式简化代码, 前提是JDK8开发环境, 如下:
List<Student> stus = new ArrayList<Student>(){
{
add(new Student("张三", 30));
add(new Student("李四", 20));
add(new Student("王五", 60));
}
};
//对users按年龄进行排序
Collections.sort(stus, (s1,s2)->(s1.getAge()-s2.getAge()));
多条件排序:
List<Student> stus = new ArrayList<Student>(){
{
add(new Student("张三", 30, 1));
add(new Student("李四", 20, 2));
add(new Student("王五", 40, 3));
add(new Student("赵六", 30, 4));
add(new Student("陈七", 40, 5));
add(new Student("周八", 20, 6));
}
};
Collections.sort(stus,new Comparator<Student>() { @Override
public int compare(Student s1, Student s2) {
int flag;
// 首选按年龄升序排序
flag = s1.getAge()-s2.getAge();
if(flag==0){
// 再按学号升序排序
flag = s1.getNum()-s2.getNum();
}
return flag;
}
}); System.out.println("年龄 学号 姓名 ");
for(Student s : stus){
System.out.println(s.getAge()+" "+s.getNum()+" "+s.getName());
}
自定义条件排序
String[] order = {"语文","数学","英语","物理","化学","生物","政治","历史","地理","总分"};
final List<String> definedOrder = Arrays.asList(order);
List<String> list = new ArrayList<String>(){
{
add("总分");
add("英语");
add("政治");
add("总分");
add("数学");
}
};
Collections.sort(list,new Comparator<String>() { @Override
public int compare(String o1, String o2) {
int io1 = definedOrder .indexOf(o1);
int io2 = definedOrder .indexOf(o2);
return io1-io2;
}
}); for(String s:list){
System.out.print(s+" ");
}
使用lambda表达式简化代码:
Collections.sort(list, (o1, o2)->(definedOrder .indexOf(o1)-definedOrder .indexOf(o2)));