PriorityQueue

A priority queue is a special type of queue in which each element is associated with a priority and is served according to its priority. In Java, the PriorityQueue class is an implementation of a priority queue. Here is an example of how to use the PriorityQueue class:

import java.util.PriorityQueue;

public class PriorityQueueExample {
    public static void main(String[] args) {
        // Create a PriorityQueue
        PriorityQueue<Integer> pQueue = new PriorityQueue<>();

        // Adding items to the pQueue using add()
        pQueue.add(10);
        pQueue.add(20);
        pQueue.add(15);
        pQueue.add(30);

        // Printing the top element of PriorityQueue
        System.out.println("Top element is: " + pQueue.peek());

        // Printing the top element and removing it
        // from the PriorityQueue container
        System.out.println("Removed element is: " + pQueue.poll());

        // Printing the top element again
        System.out.println("Top element is: " + pQueue.peek());
    }
}
Top element is: 10
Removed element is: 10
Top element is: 15

In this example, we create an instance of PriorityQueue class and add several elements using the add() method. The add() method adds the element to the queue and assigns a priority to it based on its natural ordering.

We use the peek() method to retrieve the top element of the queue, which is the element with the highest priority. The poll() method retrieves and removes the top element of the queue. We can see that after removing the top element, the next element with the highest priority becomes the top element.

It’s worth noting that the PriorityQueue doesn’t accept null element, and it is not thread-safe, to make it thread-safe you can use PriorityBlockingQueue instead.

Also, the PriorityQueue class uses the natural ordering of the elements, if the elements in the queue are not comparable, you can provide a comparator while creating the PriorityQueue to define the ordering of the elements.

Here is an example of how to use a comparator with the PriorityQueue class:

import java.util.Comparator;
import java.util.PriorityQueue;

public class PriorityQueueExample {
    public static void main(String[] args) {
        // Create a PriorityQueue with a custom comparator
        PriorityQueue<Integer> pQueue = new PriorityQueue<>(new MyComparator());

        // Adding items to the pQueue
        pQueue.add(10);
        pQueue.add(20);
        pQueue.add(15);
        pQueue.add(30);

        // Printing the top element of PriorityQueue
        System.out.println("Top element is: " + pQueue.peek());

        // Printing the top element and removing it
        // from the PriorityQueue container
        System.out.println("Removed element is: " + pQueue.poll());

        // Printing the top element again
        System.out.println("Top element is: " + pQueue.peek());
    }

    // Custom comparator
    static class MyComparator implements Comparator<Integer> {
        public int compare(Integer x, Integer y) {
            // Compare elements in descending order
            return y - x;
        }
    }
}
Top element is: 30
Removed element is: 30
Top element is: 20

In this example, we create an instance of the PriorityQueue class, and pass an instance of MyComparator to the constructor. The MyComparator class implements the Comparator interface and provides a custom implementation of the compare() method. In this case, it compares the elements in descending order.

It’s worth noting that, the elements in the queue are ordered according to the provided comparator, if two elements have the same priority, their ordering is determined by the natural ordering of the elements.

Also, you can use the PriorityQueue along with the Lambda expression or the method reference to create a comparator, for example, you can create a comparator like this:

PriorityQueue<Integer> pQueue = new PriorityQueue<>((x,y)->y-x)

Leave a Reply

Your email address will not be published. Required fields are marked *