-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathQueueOrdering.java
More file actions
35 lines (30 loc) · 1.26 KB
/
QueueOrdering.java
File metadata and controls
35 lines (30 loc) · 1.26 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
package priorityQueue;
import java.util.Iterator;
import java.util.PriorityQueue;
import java.util.Queue;
public class QueueOrdering {
public static void main(String[] args) {
PriorityQueue<String> testStringsPQ = new PriorityQueue<>();
testStringsPQ.add("fortune");
testStringsPQ.add("dozie");
testStringsPQ.add("precious");
testStringsPQ.add("benneth");
testStringsPQ.add("imeh");
System.out.println("Strings Stored in Natural Ordering in a Priority Queue\n");
System.out.println("Using Iterator");
Iterator itr = testStringsPQ.iterator();
while (itr.hasNext()) {
System.out.println(itr.next());
}
System.out.println("\nUsing for loop");
for (String i : testStringsPQ)
System.out.println(i);
System.out.println("peek " +testStringsPQ.peek()); //sees the first member of the queue
System.out.println("\nUsing poll to empty the queue");
//Poll with while loop eventually removes all members of queue
while (!testStringsPQ.isEmpty()) {
System.out.println(testStringsPQ.poll());
}
System.out.println("peek " +testStringsPQ.peek()); //peek here returns null as queue is empty after poll
}
}