Java: Change priorityQueue to maximum priorityqueue
Java Collection, PriorityQueue Exercises: Exercise-12 with Solution
Write a Java program to change priorityQueue to maximum priority queue.
Sample Solution:-
Java Code:
import java.util.*;
public class Example12 {
public static void main(String[] args) {
PriorityQueue<Integer> pq1 = new PriorityQueue<>(10, Collections.reverseOrder());
// Add numbers in the Queue
pq1.add(10);
pq1.add(22);
pq1.add(36);
pq1.add(25);
pq1.add(16);
pq1.add(70);
pq1.add(82);
pq1.add(89);
pq1.add(14);
System.out.println("\nOriginal Priority Queue: "+pq1);
System.out.print("\nMaximum Priority Queue: ");
Integer val = null;
while( (val = pq1.poll()) != null) {
System.out.print(val+" ");
}
System.out.print("\n");
}
}
Sample Output:
Original Priority Queue: [89, 82, 70, 25, 16, 22, 36, 10, 14] Maximum Priority Queue: 89 82 70 36 25 22 16 14 10
Java Code Editor:
Contribute your code and comments through Disqus.
Previous: Convert a Priority Queue elements to a string representation.
Next: Associate the specified value with the specified key in a HashMap.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/java-exercises/collection/java-collection-priority-queue-exercise-12.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics