Priority Queues are used very often in real life applications. In this article we will learn what priority queues are and how we can use them in Java.

Before we discuss what a priority queue is, let’s see what a regular queue is.

A regular queue follows a first in first out ( FIFO ) structure. This means that if 3 messages – m1, m2 and m3 – go into the queue in that order, then they come out of the queue in the exact same order.

Why do we need queues?

Let us say that we have data producers ( for example, when a user clicks on a web page ) which are extremely fast. But then we want to consume this data at a slower pace later.

In this case, the producer would push all of the messages into the queue, and a consumer would consume these messages later from the queue at a slower pace.

What is a priority queue?

As mentioned earlier, a regular queue has a first in first out structure. But in some scenarios we want to process messages in a queue based on their priority and not based on when the message entered the queue.

Priority queues help consumers consume the higher priority messages first followed by the lower priority messages.

Priority queues in Java

Now let’s see some actual Java code that will show us how to use priority queues.

Priority queues with natural ordering

Here is some code showing how to create a simple priority queue for strings

private static void testStringsNaturalOrdering() {
        Queue<String> testStringsPQ = new PriorityQueue<>();
        testStringsPQ.add("abcd");
        testStringsPQ.add("1234");
        testStringsPQ.add("23bc");
        testStringsPQ.add("zzxx");
        testStringsPQ.add("abxy");

        System.out.println("Strings Stored in Natural Ordering in a Priority Queue\n");
        while (!testStringsPQ.isEmpty()) {
            System.out.println(testStringsPQ.poll());
        }
    }

The first line tells us that we are creating a priority queue:

Queue<String> testStringsPQ = new PriorityQueue<>();

PriorityQueue is available in java.util package.

Next we are adding 5 strings in random order into the priority queue. For this we use the add() function as shown below:

testStringsPQ.add("abcd");
testStringsPQ.add("1234");
testStringsPQ.add("23bc");
testStringsPQ.add("zzxx");
testStringsPQ.add("abxy");

In order to get the latest item from the queue we use the poll() function as shown below:

testStringsPQ.poll()

poll() will give us the latest item and also remove it from the queue. If we want to get the latest item in the queue without removing it, we can use the peek() function:

testStringsPQ.peek()

Finally, we print out all the elements from the queue by using the poll() function as shown below:

while (!testStringsPQ.isEmpty()) {
   System.out.println(testStringsPQ.poll());
}

Here is the output of the above program:

1234
23bc
abcd
abxy
zzxx

Since we did not tell the priority queue how to prioritize its content, it used a default natural ordering. In this case, it gave us the data back in the ascending order of the strings. This is not the same order in which items were added to the queue.

#java #developer

Priority Queues in Java Explained with Examples
3.05 GEEK