-
Notifications
You must be signed in to change notification settings - Fork 0
/
comparator.java
89 lines (72 loc) · 2.56 KB
/
comparator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// 比较器的构造:
// 一个系统提供的有序的结构,都会伴随着比较器的构造,这个比较器告诉这个有序结构如何组织!! -> 可以用排序,优先级队列,红黑树
// 基于比较的排序
import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
import Code_09_Comparator.Student;
public class comparator {
// 主函数
public static void main(String[] args) {
Student student1 = new Student("A", 1, 23);
Student student2 = new Student("B", 2, 21);
Student student3 = new Student("C", 3, 22);
Student[] students = new Student[] {student3, student2, student1};
printStudent(students);
Arrays.sort(students, new IdAscendingComparator());
printStudent(students);
Arrays.sort(students, new IdDescendingComparator());
printStudent(students);
// 堆的方式:
// 系统提供的一个堆(优先级队列) // 如果不给比较器,它默认按内存地址排序,没有意思, 需要仍进去比较器
// 这里如果扔进去,id小的排在前面,则这是一个小根堆, -> 这里通过比较器构成了小根堆
System.out.println("*********************************************");
PriorityQueue<Student> heap = new PriorityQueue<>(new IdAscendingComparator());
heap.add(student3);
heap.add(student2);
heap.add(student1);
while (!heap.isEmpty()) {
Student student = heap.poll(); // 弹出堆
System.out.println("name: " + student.name +" "+ "id: " + student.id + " "+"Age: " + student.age );
}
}
// 1) 创建Student类, 内部包含3个变量,名字,id,年龄
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.id = id;
this.age = age;
}
}
// 2) 创建一个比较器类 -> id升序,继承
public static class IdAscendingComparator implements Comparator<Student>{
@Override
public int compare(Student o1, Student o2) {
// return o1.id - o2.id;
if(o1.id < o2.id) {
return -1; // 表示o1更小, 因为o1.id - o2.id < 0 , 为负数;第一个参数放前面。
}else if (o1.id > o2.id) {
return 1; // 表示o1更大, 因为o1.id - o2.id > 0 , 为正数;
}else {
return 0;
}
}
}
// 比较器 -> 年龄降序
public static class IdDescendingComparator implements Comparator<Student>{
@Override
public int compare(Student o1, Student o2) {
return o2.age - o1.age; // 浓缩的写法,如果o2.age - o1.age > 0, 所有o2更大。
}
}
// 3) 打印函数如图
public static void printStudent(Student[] students) {
for (Student student:students) {
System.out.println("name: " + student.name +" "+ "id: " + student.id + " "+"Age: " + student.age );
}
System.out.println("============================================");
}
}