比较器的实质就是重载比较运输符,重写比较方法,比较器可以很好的应用在特殊的标准排序上也可以应用在根据特殊标准的排序结构上,使得写代码变得异常容易,还用于泛型编程
任何比较器的统一约定
即如下方法:
@Override public int compare(T o1, T o2) ;
返回负数的情况,就是o1比o2优先的情况
返回正数的情况,就是o2比o1优先的情况 返回0的情况,就是o1与o2同样优先的情况
代码应用
public class Code01_Comparator {
//定义一个内部类
public static class Student{
public String name;
public int id;
public int age;
public Student(String name,int id , int age){
this.name = name;
this.age = age;
this.id = id;
}
}
// 任何比较器:
// compare方法里,遵循一个统一的规范:
// 返回负数的时候,认为第一个参数应该排在前面
// 返回正数的时候,认为第二个参数应该排在前面
// 返回0的时候,认为无所谓谁放前面
//id升序,如果id相等按年龄降序排序
public static class IdShengAgeJiangOrder implements Comparator<Student>{
@Override
public int compare(Student o1, Student o2) {
return o1.id != o2.id ? o1.id - o2.id :o2.age - o1.age ;
}
}
//id升序排序
public static class IdAscendingComparator implements Comparator<Student>{
@Override
public int compare(Student o1, Student o2) {
return o1.id - o2.id;
}
}
// 先按照id排序,id小的,放前面;
// id一样,age大的,前面;
public static class IdInAgeDe implements Comparator<Student> {
@Override
public int compare(Student o1, Student o2) {
return o1.id != o2.id ? o1.id - o2.id : (o2.age - o1.age);
}
}
//打印学生的信息
public static void printStudents(Student[] students){
for (Student student : students) {
System.out.println("Name : " + student.name + ",Id : " + student.id + ",Age :" +student.age);
}
}
// 打印数组
public static void printArray(Integer[] arr){
if (arr == null){
return;
}
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
System.out.println();
}
}
public static class MyComp implements Comparator<Integer> {
@Override
public int compare(Integer o1, Integer o2) {
return o2 - o1;
}
}
public static class AComp implements Comparator<Integer> {
// 如果返回负数,认为第一个参数应该拍在前面
// 如果返回正数,认为第二个参数应该拍在前面
// 如果返回0,认为谁放前面都行
@Override
public int compare(Integer arg0, Integer arg1) {
return arg1 - arg0;
// return 0;
}
}
public static void main(String[] args) {
//创建一个数组
Integer [] arr = {5, 3, 3, 1, 2, 7 ,8, 9};
//排序,传入数组和自定义比较器
Arrays.sort(arr,new AComp());
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
System.out.println("===========================");
Student student1 = new Student("A", 4, 40);
Student student2 = new Student("B", 4, 21);
Student student3 = new Student("C", 3, 12);
Student student4 = new Student("D", 3, 62);
Student student5 = new Student("E", 3, 42);
Student[] students = new Student[] { student1, student2, student3, student4, student5 };
System.out.println("第一条打印");
//排序
Arrays.sort(students,new IdShengAgeJiangOrder());
//打印排序好的数组
printStudents(students);
System.out.println("第二条打印");
ArrayList<Student> studentList = new ArrayList<>();
studentList.add(student1);
studentList.add(student2);
studentList.add(student3);
studentList.add(student4);
studentList.add(student5);
//排序
studentList.sort(new IdShengAgeJiangOrder());
for (int i = 0; i < studentList.size(); i++) {
Student s = studentList.get(i);
System.out.println(s.name + "," + s.id + "," + s.age);
}
System.out.println("第三条打印");
student1 = new Student("A", 4, 40);
student2 = new Student("B", 4, 21);
student3 = new Student("C", 4, 12);
student4 = new Student("D", 4, 62);
student5 = new Student("E", 4, 42);
TreeMap<Student, String> treeMap = new TreeMap<>((a, b) -> (a.id - b.id));
treeMap.put(student1, "我是学生1,我的名字叫A");
treeMap.put(student2, "我是学生2,我的名字叫B");
treeMap.put(student3, "我是学生3,我的名字叫C");
treeMap.put(student4, "我是学生4,我的名字叫D");
treeMap.put(student5, "我是学生5,我的名字叫E");
for (Student s : treeMap.keySet()) {
System.out.println(s.name + "," + s.id + "," + s.age);
}
}
}
运行结果
9
8
7
5
3
3
2
1
===========================
第一条打印
Name : D,Id : 3,Age :62
Name : E,Id : 3,Age :42
Name : C,Id : 3,Age :12
Name : A,Id : 4,Age :40
Name : B,Id : 4,Age :21
第二条打印
D,3,62
E,3,42
C,3,12
A,4,40
B,4,21
第三条打印
A,4,40
Disconnected from the target VM, address: '127.0.0.1:56201', transport: 'socket'