Queue & PriorityQueue

Java Queue Interface and PriorityQueue

Last Updated : 28 Mar 2026

Java Queue Interface is used to store elements in a specific order, usually following the First-In-First-Out (FIFO) principle. It is commonly used in real-life scenarios like task scheduling and order processing. You will also learn different examples to understand how Queue works in Java.

In this chapter, you will learn about the Java Queue interface, its features, working, and examples.

What is Queue Interface in Java?

The Queue interface belongs to the java.util package and is a part of the Java Collections Framework. It is used to store elements in an ordered way where elements are added at the end and removed from the beginning.

Queue follows the FIFO (First-In-First-Out) principle, which makes it useful for applications like task scheduling, print queues, and order management.

Queue is an interface, so it cannot be created directly. It is implemented by classes like LinkedList and PriorityQueue.

Queue Interface Declaration

The Queue interface is declared as an interface that extends the Collection interface.

Here is the syntax:

Methods of Java Queue Interface

The following table shows the commonly used methods of the Queue interface:

MethodDescription
boolean add(object)It is used to insert the specified element into this queue and return true upon success.
boolean offer(object)It is used to insert the specified element into this queue.
Object remove()It is used to retrieves and removes the head of this queue.
Object poll()It is used to retrieves and removes the head of this queue, or returns null if this queue is empty.
Object element()It is used to retrieves, but does not remove, the head of this queue.
Object peek()It is used to retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.

Features of a Queue

The following are some important features of a queue.

  • As discussed earlier, FIFO concept is used for insertion and deletion of elements from a queue.
  • The Java Queue provides support for all of the methods of the Collection interface including deletion, insertion, etc.
  • PriorityQueue, ArrayBlockingQueue and LinkedList are the implementations that are used most frequently.
  • The NullPointerException is raised, if any null operation is done on the BlockingQueues.
  • Those Queues that are present in the util package are known as Unbounded Queues.
  • Those Queues that are present in the util.concurrent package are known as bounded Queues.
  • All Queues barring the Deques facilitates removal and insertion at the head and tail of the queue; respectively. In fact, deques support element insertion and removal at both ends.

PriorityQueue Class

PriorityQueue is a class in the Java Collection Framework that is used to process elements based on priority.

In a normal queue, elements follow the FIFO (First-In-First-Out) order. However, in PriorityQueue, elements are processed according to their priority instead of insertion order.

PriorityQueue Class Declaration

The PriorityQueue class extends AbstractQueue and implements Serializable.

Here is the syntax:

PriorityQueue Example

Here are some examples of the Java PriorityQueue class to understand its usage.

Example 1: PriorityQueue with String Elements

In this example, we are creating a PriorityQueue, adding elements, and performing different operations like peek(), remove(), and poll().

Output:

head:Amit
head:Amit
iterating the queue elements:
Amit
Jai
Karan
Vijay
Rahul
after removing two elements:
Karan
Rahul
Vijay

Example 2: PriorityQueue with User-Defined Objects

In this example, we are storing user-defined Book objects in a PriorityQueue using the Comparable interface.

Output:

Traversing the queue elements:
101 Data Communications & Networking Forouzan Mc Graw Hill 4
233 Operating System Galvin Wiley 6
121 Let us C Yashwant Kanetkar BPB 8
After removing one book record:
121 Let us C Yashwant Kanetkar BPB 8
233 Operating System Galvin Wiley 6