Repository navigation
Expand file tree
/
Copy pathShowComparator2.java
More file actions
77 lines (70 loc) · 2.11 KB
/
Copy pathShowComparator2.java
File metadata and controls
77 lines (70 loc) · 2.11 KB
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
package Novice_class;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.TreeSet;
public class ShowComparator2 {
static class Student {
// 属性
String name;
int age;
int id;
// 方法
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", id=" + id +
'}';
}
// 构造器
public Student() {
}
public Student(String name, int id, int age) {
this.name = name;
this.id = id;
this.age = age;
}
}
// age小的放在前面
static class IdComparator implements Comparator<Student> {
@Override
public int compare(Student o1, Student o2) {
if(o1.age < o2.age) {
return -1;
}else if(o1.age > o2.age) {
return 1;
}else {
return 0;
}
}
}
public static void main(String[] args) {
PriorityQueue<Student> heap = new PriorityQueue<>(new IdComparator());
Student s1 = new Student("张三",1,20);
Student s2 = new Student("李四",2,23);
Student s3 = new Student("王五",3,17);
Student s4 = new Student("老六",4,52);
Student s5 = new Student("哈奇",5,34);
heap.add(s1);
heap.add(s2);
heap.add(s3);
heap.add(s4);
heap.add(s5);
int length = heap.size();
for (int i = 0; i < length; i++) {
System.out.println(heap.poll());
}
TreeSet<Student> treeSet = new TreeSet<>(new IdComparator());
treeSet.add(s1);
treeSet.add(s2);
treeSet.add(s3);
treeSet.add(s4);
treeSet.add(s5);
length = treeSet.size();
System.out.println("-----------------");
for (int i = 0; i < length; i++) {
System.out.println(treeSet.pollFirst());
}
}
}