-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPriorityQueueDemo.java
More file actions
53 lines (43 loc) · 1.63 KB
/
PriorityQueueDemo.java
File metadata and controls
53 lines (43 loc) · 1.63 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
package holding;
/**
* RUN:
* javac holding/PriorityQueueDemo.java && java holding.PriorityQueueDemo
* OUTPUT:
*
*/
import java.util.*;
public class PriorityQueueDemo {
public static void printQ(Queue queue) {
while (queue.peek() != null) {
System.out.print(queue.remove() + " ");
}
System.out.println();
}
public static void main(String[] args) {
PriorityQueue<Integer> priorityQueue = new PriorityQueue<Integer>();
Random rand = new Random(47);
for (int i = 0; i < 10; i++) {
priorityQueue.offer(rand.nextInt(i+10));
}
printQ(priorityQueue);
List<Integer> ints = Arrays.asList(25, 22, 20, 18, 14, 9, 3, 1, 1, 2, 3, 9, 14, 18, 21, 23, 25);
priorityQueue = new PriorityQueue<Integer>(ints);
printQ(priorityQueue);
priorityQueue = new PriorityQueue<Integer>(ints.size(), Collections.reverseOrder());
priorityQueue.addAll(ints);
printQ(priorityQueue);
String fact = "EDUCATION SHOULD ESCHEW OBFUSCATION";
List<String> strings = Arrays.asList(fact.split(""));
PriorityQueue<String> stringPQ = new PriorityQueue<String>(strings);
printQ(stringPQ);
stringPQ = new PriorityQueue<String>(strings.size(), Collections.reverseOrder());
stringPQ.addAll(strings);
printQ(stringPQ);
Set<Character> charSet = new HashSet<Character>();
for (char c : fact.toCharArray()) {
charSet.add(c);
}
PriorityQueue<Character> characterPQ = new PriorityQueue<Character>(charSet);
printQ(characterPQ);
}
}