Implement different operations on Priority Queue by adding element, removing element, size of priority queue, print the queue and top element of queue in java code example
Example: priority queue in java
import java.util.PriorityQueue;
import java.util.Iterator;
class Main {
public static void main(String[] args) {
PriorityQueue<Integer> numbers = new PriorityQueue<>();
numbers.add(4);
numbers.add(2);
numbers.add(1);
System.out.print("PriorityQueue using iterator(): ");
Iterator<Integer> iterate = numbers.iterator();
while(iterate.hasNext()) {
System.out.print(iterate.next());
System.out.print(", ");
}
}
}