Java Program to Implement PriorityQueue API Last Updated : 22 Jul, 2021 Comments Improve Suggest changes Like Article Like Report A PriorityQueue is a linear data structure in which the elements are ordered according to their natural ordering or by some custom comparator provided at the queue at construction time. In PriorityQueue, the front of the queue points to the least element, and the rear points to the greatest element according to the natural Ordering. For alphabetical PriorityQueue, their ASCII values will be considered for ordering. Some important characteristics of PriorityQueue are as follows: It does not allow the null elements to be inserted.It is an unbounded Queue that means its size can be expanded.It inherits the classes like Object, Abstract Collection, AbstractQueue.It is not thread-safe.It cannot be created for non-comparable objects. Time Complexities of various operations: Insertion and deletion are f order O(log(n))remove() and contains() method is of order O(n)Retrieval operations are the fastest which are of order O(1) The PriorityQueue class inherits Queue Interface and all of its methods. PriorityQueue API implements serializable, Iterable, Collection, and Queue which can be perceived from the architecture shown below. Serializable, Iterable<E>, Collection<E>, Queue<E> Syntax: public class PriorityQueue<E> extends AbstractQueue<E> implements Serializable Parameter: E — The type of elements held in this queue. Methods: Method Type Description add(E e) boolean Inserts an element e to the PriorityQueueclear() void Removes all the elements from the PriorityQueue contains(Object O) booleanReturn true if it contains the specified elementiterator() Iterator<E>Returns an iterator over all the elementsremove(Object o) booleanRemoves the specified element from the Queuecomparator() Comparator<E> Returns the custom comparator used to order th elementstoArray() Object[] Returns an array that contains all the elements in the PriorityQueue.peek() <E> Return the head of the PriorityQueue without deleting the element from the Queuepoll() <E> Removes and returns the head of the queue. Returns null if the queue is empty.spliterator() Spliterator<E> Creates a late-binding and fail-fast Spliterator over the elements in the PriorityQueue. Implementation: Example Java // Java Program to implement Priority Queue API // Importing all classes from java.util package import java.util.*; // Class class GFG { // Main driver method public static void main(String[] args) { // Creating(Declaring) an object of PriorityQueue of // Integer type i.e Integer elements will be // inserted in above object PriorityQueue<Integer> pq = new PriorityQueue<>(); // Adding elements to the object created above // Custom inputs pq.add(89); pq.add(67); pq.add(78); pq.add(12); pq.add(19); // Printing the head of the PriorityQueue // using peek() method of Queues System.out.println("PriorityQueue Head:" + pq.peek()); // Display message System.out.println("\nPriorityQueue contents:"); // Defining the iterator to traverse over elements of // object Iterator i = pq.iterator(); // Condition check using hasNext() method which hold // true till single element is remaining in List while (i.hasNext()) { // Printing the elements of object System.out.print(i.next() + " "); } // Removing random element from above elements added // from the PriorityQueue // Custom removal be element equals 12 pq.remove(12); // Display message System.out.print("\nPriorityQueue contents:"); // Declaring iterator to traverse over object // elements Iterator it = pq.iterator(); // Condition check using hasNext() method which hold // true till single element is remaining in List while (it.hasNext()) { // Printing the elements System.out.print(it.next() + " "); } // Removing all the elements from the PriorityQueue // using clear() method pq.clear(); // Adding another different set of elements // to the Queue object // Custom different inputs pq.add(5); pq.add(7); pq.add(2); pq.add(9); // Checking a random element just inserted // using contains() which returns boolean value System.out.print("The queue has 7 = " + pq.contains(7)); // Display message for content in Priority queue System.out.print("\nPriorityQueue contents:"); // Converting PriorityQueue to array // using toArray() method Object[] arr = pq.toArray(); // Iterating over the array elements for (int j = 0; j < arr.length; j++) { // Printing all the elements in the array System.out.print(arr[j] + " "); } } } OutputPriorityQueue Head:12 PriorityQueue contents: 12 19 78 89 67 PriorityQueue contents:19 67 78 89 The queue has 7 = true PriorityQueue contents:2 7 5 9 Comment More infoAdvertise with us Next Article Java Program to Implement PriorityQueue API N namitachaudhary60 Follow Improve Article Tags : Java Java Programs Java-Collections java-priority-queue Practice Tags : JavaJava-Collections Similar Reads Java Program to Implement PriorityBlockingQueue API PriorityBlockingQueue is an unbounded blocking queue that uses the same ordering rules as class PriorityQueue and supplies blocking retrieval operations. The âblockingâ part of the name is added to imply the thread will block waiting until thereâs an item available on the queue. This class does not 2 min read Java Program to Implement LinkedBlockingQueue API LinkedBlockingQueue API is an optionally-bounded queue based on linked nodes. It orders the elements in FIFO(First In First Out) order. The head of this queue is the element that has been there in the queue for the longest time and the tail of the queue is the element that has been in the queue for 6 min read Java Program to Implement LinkedTransferQueue API LinkedTransferQueue is a queue that orders elements FIFO (first-in-first-out) with respect to any given producer. The head of the queue is that element that has been on the queue the longest time for some producer. The tail of the queue is that element that has been on the queue the shortest time fo 5 min read Java Program to Implement SynchronousQueue API SynchronousQueue is a special blocking queue with no internal capacity. It helps in exchange data or information between threads in a thread-safe manner. SynchronousQueue has only 2 supported operations: Both of these are blocking method which means when we want to add a piece of information or data 3 min read Java Program to Implement DelayQueue API The DelayQueue class belongs to java.util.concurrent package. DelayQueue implements the BlockingQueue interface. DelayQueue is a specialized Priority Queue that orders elements supported their delay time. It means that only those elements can be taken from the queue whose time has expired. DelayQueu 7 min read How to Remove the Top Element From a PriorityQueue in Java? A priority queue is a specialized type of queue, in which elements are given with priorities, and those with more priorities are served before elements with lower priorities. It's equivalent to a regular queue (first-in, first-out) but prioritizes urgency rather than arrival order. Remove the Top El 2 min read Java Program to Implement ArrayBlockingQueue API ArrayBlockingQueue class is a member of the Java Collection framework. ArrayBlockingQueue is a bounded blocking queue. The term bounded, means that the size of the Queue is fixed and cannot be changed. Any attempt to put element/elements into a full queue will lead to blocking operation. Similarly, 7 min read How to Copy Elements from One PriorityQueue to Another in Java? In Java, a priority queue is a data structure that allows the users to store data based on their priority. In this article, we will learn how to copy elements from one priority queue to another in Java. Example Input: PriorityQueue original ={1,2,3,4,5}Output: PriorityQueue Copy ={1,2,3,4,5}Copy ele 2 min read Java Program to Handle Duplicate Elements in a PriorityQueue While Maintaining Order In Java, elements in a Priority Queue are arranged according to their priority, which is based on a priority heap. When there are duplicate items, the default comparison method is to use a comparator that is given or to compare the elements according to their natural ordering. We can use a custom co 3 min read Java Program to Implement ConcurrentLinkedQueue API The ConcurrentLinkedQueue class in Java is a part of the Java Collection Framework. It belongs to java.util.concurrent package. It was introduced in JDK 1.5. It is used to implement Queue with the help of LinkedList concurrently. It is an unbounded thread-safe implementation of Queue which inserts e 4 min read Like