-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThePriorityQueue.java
More file actions
39 lines (35 loc) · 1.37 KB
/
Copy pathThePriorityQueue.java
File metadata and controls
39 lines (35 loc) · 1.37 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
// Priority Queue = FIFO data struture that serves elements
// With Highest priorities first
// Before elements with lower priorities
import java.util.Queue;
import java.util.Collections;
import java.util.PriorityQueue;
public class ThePriorityQueue {
public static void main(String[] args) {
// ==> Priority queue for doubles
// Queue<Double> priorityQueueA = new PriorityQueue<>(); //Ascending
// (or min-heap)
Queue<Double> priorityQueueA = new PriorityQueue<>(Collections.reverseOrder()); // This method is used to change
// the natural ordering of
// elements in a PriorityQueue to
// descending (or max-heap)
priorityQueueA.offer(3.40);
priorityQueueA.offer(4.00);
priorityQueueA.offer(2.07);
priorityQueueA.offer(1.00);
priorityQueueA.offer(2.00);
while (!priorityQueueA.isEmpty()) {
System.out.println(priorityQueueA.poll());
}
// ==> Priority queue for strings
Queue<String> priorityQueueB = new PriorityQueue<>();
priorityQueueB.offer("C");
priorityQueueB.offer("A");
priorityQueueB.offer("R");
priorityQueueB.offer("R");
priorityQueueB.offer("F");
while (!priorityQueueB.isEmpty()) {
System.out.println(priorityQueueB.poll());
}
}
}