-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_121pQueue.java
More file actions
36 lines (35 loc) · 1.02 KB
/
_121pQueue.java
File metadata and controls
36 lines (35 loc) · 1.02 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
import java.util.*;
public class _121pQueue {
static class Student implements Comparable<Student>{
String name;
int rank;
public Student(String name , int rank){
this.name = name;
this.rank = rank;
}
@Override
public int compareTo(Student s2){
return this.rank-s2.rank;
}
}
public static void main(String[] args) {
PriorityQueue<Integer> pq = new PriorityQueue<>(Comparator.reverseOrder());
pq.add(3);
pq.add(4);
pq.add(1);
pq.add(7);
while(!pq.isEmpty()){
System.out.println(pq.peek());
pq.remove();
}
PriorityQueue<Student> pq2 = new PriorityQueue<>();
pq2.add(new Student("A",4));
pq2.add(new Student("B",5));
pq2.add(new Student("C",2));
pq2.add(new Student("D",12));
while(!pq2.isEmpty()){
System.out.println(pq2.peek().name+" -> "+pq2.peek().rank);
pq2.remove();
}
}
}