| 6 | |
| 7 | public class PriorityQueueDemo { |
| 8 | public static void main(String[] args) { |
| 9 | PriorityQueue<Integer> priorityQueue = |
| 10 | new PriorityQueue<>(); |
| 11 | Random rand = new Random(47); |
| 12 | for(int i = 0; i < 10; i++) |
| 13 | priorityQueue.offer(rand.nextInt(i + 10)); |
| 14 | QueueDemo.printQ(priorityQueue); |
| 15 | |
| 16 | List<Integer> ints = Arrays.asList(25, 22, 20, |
| 17 | 18, 14, 9, 3, 1, 1, 2, 3, 9, 14, 18, 21, 23, 25); |
| 18 | priorityQueue = new PriorityQueue<>(ints); |
| 19 | QueueDemo.printQ(priorityQueue); |
| 20 | priorityQueue = new PriorityQueue<>( |
| 21 | ints.size(), Collections.reverseOrder()); |
| 22 | priorityQueue.addAll(ints); |
| 23 | QueueDemo.printQ(priorityQueue); |
| 24 | |
| 25 | String fact = "EDUCATION SHOULD ESCHEW OBFUSCATION"; |
| 26 | List<String> strings = |
| 27 | Arrays.asList(fact.split("")); |
| 28 | PriorityQueue<String> stringPQ = |
| 29 | new PriorityQueue<>(strings); |
| 30 | QueueDemo.printQ(stringPQ); |
| 31 | stringPQ = new PriorityQueue<>( |
| 32 | strings.size(), Collections.reverseOrder()); |
| 33 | stringPQ.addAll(strings); |
| 34 | QueueDemo.printQ(stringPQ); |
| 35 | |
| 36 | Set<Character> charSet = new HashSet<>(); |
| 37 | for(char c : fact.toCharArray()) |
| 38 | charSet.add(c); // Autoboxing |
| 39 | PriorityQueue<Character> characterPQ = |
| 40 | new PriorityQueue<>(charSet); |
| 41 | QueueDemo.printQ(characterPQ); |
| 42 | } |
| 43 | } |
| 44 | /* Output: |
| 45 | 0 1 1 1 1 1 3 5 8 14 |