Open In App

Should we declare as Queue or Priority Queue while using Priority Queue in Java?

Last Updated : 21 Nov, 2022
Like Article


Queue is an Interface that extends the collection Interface in Java and this interface belongs to java.util package

A queue is a type of data structure that follows the FIFO (first-in-first-out ) order. The queue contains ordered elements where insertion and deletion of elements are done at different ends. 

Priority Queue and Linked List are some common classes that implement the Queue interface.

Syntax to declare Queue:

public interface Queue<E> extends Collection<E>

Some methods which can be performed on Queue interface are add( ), poll( ), peek( ), remove( ) and more.


  • Queues can be used to implement other data structures like arrays and lists.
  • Queues are very easy to use and implement.
  • Easy to manage and operate on a huge amount of data.


  • Queues have limited size as they are predefined.
  • Searching for data in Queues is not easy.


Priority Queue is a class that is defined in the java collection framework and implements the Queue interface. It also follows FIFO order but in this case elements in the queue are placed according to their priorities.

Syntax to declare Priority Queue:

public class PriorityQueue<E> extends AbstractQueue<E> implements Serializable  


  • Searching of data is easy as they are placed according to their priority.
  • PriorityQueues are more efficiently organized


  • Performing insertion operations on a priority queue take more time as they need to find the positions of elements according to their priority.
Java Framework for queue and priority queue

Java Framework for queue and priority queue

Here let us see whether we can use Queue or priority queue when we are using priority queue.

Case – 1

PriorityQueue<Integer> pq = new PriorityQueue<>(); 

Advantages: By declaring the priority queue as priority queue we can quickly access the highest item with a time complexity of just O(1).

Disadvantages: Enqueue and dequeue operations on the priority queue are slow and also have a time complexity of O(log n).

Case – 2:

Queue<Integer> q = new PriorityQueue<>();

Advantages: By defining a priority queue as Queue we can change the actual implementation with a single line.

Disadvantages: In some cases, it will generate errors if our code includes priority functionalities where prioritization methods are absent because of declaring the priority queue as Queue.


In many cases, the first case is mostly used but while writing our code if Queue do not use any functionality of prioritization then we can use Queue. In that way, we can change the actual implementation with one line. But if our code uses any priority functionalities then we must use the first case instead of the second. If we use the second case in the usage of priority features then it will produce errors as it will call for prioritization methods. In general, the first case should be used to avoid errors.

The efficient way of declaring PriorityQueue is case 1 which is to declare a priority queue as priorityQueue because it improves code readability and avoids causing errors. 

Similar Reads

What is Priority Queue | Introduction to Priority Queue
A priority queue is a type of queue that arranges elements based on their priority values. Elements with higher priority values are typically retrieved before elements with lower priority values. In a priority queue, each element has a priority value associated with it. When you add an element to the queue, it is inserted in a position based on its
25 min read
Why can't a Priority Queue wrap around like an ordinary Queue?
Priority Queue: A priority queue is a special type of queue in which each element is assigned a priority value. And elements are served based on their priority. This means that elements with higher priority are served first. However, if elements with the same priority occur, they will be served in the order in which they were queued. A priority que
3 min read
Can we use Simple Queue instead of Priority queue to implement Dijkstra's Algorithm?
What is Dijkstra's Algorithm? Dijkstra's Algorithm is used for finding the shortest path between any two vertices of a graph. It uses a priority queue for finding the shortest path. For more detail, about Dijkstra's Algorithm, you can refer to this article. Why Dijkstra's Algorithm uses a Priority Queue? We use min heap in Dijkstra's Algorithm beca
2 min read
Turn a Queue into a Priority Queue
What is Queue?Queue is an abstract data type that is open at both ends. One end is always used to insert data (enqueue) which is basically the rear/back/tail end and the other which is the front end is used to remove data (dequeue). Queue follows First-In-First-Out (FIFO) methodology, i.e., "the data item stored first will be accessed first". Decla
9 min read
Why does Queue have front but Priority-queue has top in stl?
Why does the queue have front but the priority queue has top in stl? The main difference between a queue and a priority queue is that a queue follows the FIFO (First-In-First-Out) principle, while a priority queue follows a specific priority order. In other words, the elements in a queue are processed in the order they were added, whereas the eleme
8 min read
Difference between Circular Queue and Priority Queue
Circular queue: Circular Queue is a linear data structure in which the operations are performed based on FIFO (First In First Out) principle and the last position is connected back to the first position to make a circle. It is also called ‘Ring Buffer’. Code: [GFGTABS] C++ #include &lt;iostream&gt; using namespace std; #define SIZE 5 // Define the
7 min read
What will happen if i declare main() in java as non-static?
main() method in Java acts as an application's entry point. So declaration must read "public static void main(String[] args)" to work properly . What happens if you declare main to be non-static is as follows? Java Code import java.util.Scanner; public class Sample{ public void main(String[] args){ System.out.println(&amp;quot;This is a sample code
4 min read
Priority Queue using Linked List
Implement Priority Queue using Linked Lists. push(): This function is used to insert a new data into the queue.pop(): This function removes the element with the highest priority from the queue.peek() / top(): This function is used to get the highest priority element in the queue without removing it from the queue. Priority Queues can be implemented
12 min read
Priority Queue using Doubly Linked List
Given Nodes with their priority, implement a priority queue using doubly linked list. Prerequisite : Priority Queue push(): This function is used to insert a new data into the queue.pop(): This function removes the element with the lowest priority value from the queue.peek() / top(): This function is used to get the lowest priority element in the q
11 min read
Implementation of Non-Preemptive Shortest Job First using Priority Queue
Read here for Shortest Job First Scheduling algorithm for same arrival times.Shortest job first (SJF) or shortest job next, is a scheduling policy that selects the waiting process with the smallest execution time to execute next.In this article, we will implement the Shortest Job First Scheduling algorithm (SJF) using a priority queue, so that we c
12 min read
Article Tags :
Practice Tags :