7.) Priority Queue (basic)

 Write a programme to remove smallest element from queue.

import java.util.PriorityQueue;

public class RemoveSmallestElementFromPriorityQueue {
    public static void main(String[] args) {
        // Create a priority queue
        PriorityQueue<Integer> pq = new PriorityQueue<>();

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

        // Print the priority queue before removing the smallest element
        System.out.println("Before removing the smallest element: " + pq);

        // Remove the smallest element from the priority queue
        int smallest = pq.poll();

        // Print the smallest element that was removed
        System.out.println("The smallest element that was removed: " + smallest);

        // Print the priority queue after removing the smallest element
        System.out.println("After removing the smallest element: " + pq);
    }
}

Comments