比较
Comparable 是排序接口;若一个类实现了 Comparable 接口,就意味着 “该类支持排序”。
而 Comparator 是比较器;我们若需要控制某个类的次序,可以建立一个 “该类的比较器” 来进行排序。
前者应该比较固定,和一个具体类相绑定,而后者比较灵活,它可以被用于各个需要比较功能的类使用。可以说前者属于 “静态绑定”,而后者可以 “动态绑定”。 我们不难发现:Comparable 相当于 “内部比较器”,而 Comparator 相当于 “外部比较器”
自定义的类型,谁大谁小默认情况下,系统是无法识别的,引入比较器的目的是自定义对象的比较规则。
类比较:Comparable 接口 (规则一般定义死,修改比较麻烦)
自比较:Comparator 接口 (建议使用,比较灵活)
数值用-:
s1.getScore()-s2.getScore();
String类型用compare.to():
s1.getStuName().compareTo(s2.getStuName());
若是字母比较,如:A a D排序之后为A D a,采用a1.toUpperCase().compare.to(a2.toUpperCase())
实例
1 使用集合工具类对对象进行排序(对象实现Comparable接口)
//1.定义学生类
public class Student implements Comparable<Student>{
private int id;
private String stuName;
private int score;
public Student() {
super();
}
public Student(int id, String stuName, int score) {
super();
this.id = id;
this.stuName = stuName;
this.score = score;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
@Override
public String toString() {
return "Student [id=" + id + ", stuName=" + stuName + ", score=" + score + "]";
}
//定义学生对象的比较规则 arr[j+1]-arr[j]>0
//1.如果成绩一样,按照编号进行顺序排列 Comparable
// @Override
// public int compareTo(Student stu) {
// if(stu.getScore()==this.getScore()) {
// return stu.getId()-this.getId();
// }
// return this.getScore()-stu.getScore();
// }
//2.按成绩排序,如果成绩一样,按照名字进行顺序排列
@Override
public int compareTo(Student stu) {
if(stu.getScore()==this.getScore()) {
return this.getStuName().compareTo(stu.getStuName());
}
return this.getScore()-stu.getScore();
}
}
//2.测试类中添加学生信息,然后进行排序
public static void main(String[] args) {
ArrayList<Student> list=new ArrayList<>();
list.add(new Student(3,"jack",89));
list.add(new Student(1,"rouse",79));
list.add(new Student(4,"tina",99));
list.add(new Student(2,"apple",69));
list.add(new Student(6,"oracle",89));
list.add(new Student(5,"marry",59));
System.out.println("排序之前:"+list);
Collections.sort(list);
System.out.println("排序之后:"+list);
Student stuMax=Collections.max(list);
System.out.println("最高分:"+stuMax);
Student stuMin=Collections.min(list);
System.out.println("最低分:"+stuMin);
}
运行结果:
排序之前:[Student [id=3, stuName=jack, score=89], Student [id=1, stuName=rouse, score=79], Student [id=4, stuName=tina, score=99], Student [id=2, stuName=apple, score=69], Student [id=6, stuName=oracle, score=89], Student [id=5, stuName=marry, score=59]]
排序之后:[Student [id=5, stuName=marry, score=59], Student [id=2, stuName=apple, score=69], Student [id=1, stuName=rouse, score=79], Student [id=3, stuName=jack, score=89], Student [id=6, stuName=oracle, score=89], Student [id=4, stuName=tina, score=99]]
最高分:Student [id=4, stuName=tina, score=99]
最低分:Student [id=5, stuName=marry, score=59]
2 采用匿名内部类的方式实现Comparator 接口
更加的灵活,一般会采用匿名内部类的方式自定义比较规则(写法更加的简洁)
public static void main(String[] args) {
ArrayList<Student> list=new ArrayList<>();
list.add(new Student(3,"jack",89));
list.add(new Student(1,"rouse",79));
list.add(new Student(4,"tina",99));
list.add(new Student(2,"apple",69));
list.add(new Student(6,"oracle",89));
list.add(new Student(5,"marry",59));
//1.顺序排列
Collections.sort(list,new Comparator<Student>() {
@Override
public int compare(Student stu1, Student stu2) {
return stu1.getScore()-stu2.getScore();
}
});
System.out.println("按照成绩排序之后:"+list);
//2.返回最高分
Student maxStu=Collections.max(list,new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o1.getScore()-o2.getScore();
}
});
System.out.println(maxStu);
}
运行结果:
按照成绩排序之后:[Student [id=5, stuName=marry, score=59], Student [id=2, stuName=apple, score=69], Student [id=1, stuName=rouse, score=79], Student [id=3, stuName=jack, score=89], Student [id=6, stuName=oracle, score=89], Student [id=4, stuName=tina, score=99]]
Student [id=4, stuName=tina, score=99]
比较器的练习题
创建一个商品类Goods(int id, String goodsName,int num,double price) 99.99 99.89,99.79创建一个测试类 定义集合,存入6件商品信息<1>.按照价格进行顺序排列,如果价格一样按照商品的数量排序。<2>.求出最高价格的商品,以及价格最低的商品
public class Goods {
private int id;
private String goodsName;
private int num;
private double price;
public Goods() {
}
public Goods(int id, String goodsName, int num, double price) {
this.id = id;
this.goodsName = goodsName;
this.num = num;
this.price = price;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getGoodsName() {
return goodsName;
}
public void setGoodsName(String goodsName) {
this.goodsName = goodsName;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "Goods{" +
"id=" + id +
", goodsName='" + goodsName + '\'' +
", num=" + num +
", price=" + price +
'}';
}
}
注意:比较器只能返回int类型,当比较的结果是double类型时,需进行转换判断(>0则互换位置)