2.) Priority Queue (basic)

 How to remove element from priority queue

import java.util. PriorityQueue;
public class Example {
    public static void main(String[] args) {
        // Create a priority queue of integers
        PriorityQueue<Integer> pq = new PriorityQueue<>();

        // Add elements to the priority queue
        pq.add(5);
        pq.add(3);
        pq.add(7);
        pq.add(1);

        // Print the elements of the priority queue
        System.out.println("Priority Queue before removal: " + pq);

        // Remove elements from the priority queue
        pq.remove(3);
        pq.remove(7);

        // Print the elements of the priority queue after removal
        System.out.println("Priority Queue after removal: " + pq);
    }
}


Comments