PriorityQueue class example in java

The PriorityQueue class implements the Queue interface. When items are added to a PriorityQueue they are not order by First In, First Out . Instead of, all items in a PriorityQueue must be comparable (either by implement the Comparable interface or by according to a Comparator) which is used to sort the items in the list.

Constructions in PriorityQueue class

PriorityQueue()

This construction creates a PriorityQueue with the default initial capacity (11) that orders its elements according to their natural ordering (using Comparable).

PriorityQueue(Collection<? extends E> c)

This construction creates a PriorityQueue containing the elements in the specified collection.

PriorityQueue(int initialCapacity)

This construction creates a PriorityQueue with the specified initial capacity that orders its elements according to their natural ordering (using Comparable).

PriorityQueue(int initialCapacity, Comparator<? super E> comparator)

This construction creates a PriorityQueue with the specified initial capacity that orders its elements according to the specified comparator.

PriorityQueue(PriorityQueue<? extends E> c)

This construction creates a PriorityQueue containing the elements in the specified collection.

PriorityQueue(SortedSet<? extends E> c)

This construction creates a PriorityQueue containing the elements in the specified collection.

Example of PriorityQueue class

package com.java.connect.collection;
 
import java.util.PriorityQueue;
 
public class PriorityQueueExample {
    public static void main(String[] args) {
        PriorityQueue<string> priorityQueue = new PriorityQueue<string>();
        priorityQueue.add("test1");
        priorityQueue.add("test2");
        priorityQueue.add("test3");
        priorityQueue.add("test4");
        priorityQueue.add("test5");
        System.out.println(priorityQueue);
    }
}

The output of the above code will be as:

[test1, test2, test3, test4, test5]

Example of PriorityQueue with Comparator

If you want to sort the PriorityQueue on what you want. You can use the constructor overload which takes a Comparator comparator and pass in a comparator which compares in the appropriate way for your sort order. Here we will see the example:

package com.java.connect.collection;
 
import java.util.Comparator;
import java.util.PriorityQueue;
 
public class PriorityQueueExample {
    public static void main(String[] args) {
        Comparator<string> comparator = new StringComparator();
        PriorityQueue<string> priorityQueue = new PriorityQueue<string>(10, comparator);
        priorityQueue.add("test1");
        priorityQueue.add("test2");
        priorityQueue.add("test3");
        priorityQueue.add("test4");
        priorityQueue.add("test5 test6");
        System.out.println(priorityQueue);
        for (String string : priorityQueue) {
            System.out.println("The value is : " + string);
        }
    }
}
 
class StringComparator implements Comparator<string> {
 
    public int compare(String left, String right) {
        if (left.length() < right.length()) {
            return -1;
        }
        if (left.length() > right.length()) {
            return 1;
        }
        return 0;
    }
}

The output of the above code will be as:

[test1, test2, test3, test4, test5 test6]
The value is : test1
The value is : test2
The value is : test3
The value is : test4
The value is : test5 test6