Open In App

Queue Interface In Java

Last Updated : 08 Aug, 2025
Comments
Improve
Suggest changes
2 Likes
Like
Report

The Queue Interface is a part of java.util package and extends the Collection interface. It stores and processes the data in an order where elements are added at the rear and removed from the front.

Key Features:

  • FIFO Order – Elements are processed in the order they were inserted (First-In-First-Out).
  • No Random Access – Unlike List, elements cannot be accessed directly by index.
  • Multiple Variants – Includes PriorityQueue, Deque, ArrayDeque, and LinkedList implementations.
  • Two Sets of Methods – Throws-exception versions (add, remove, element) and safe versions (offer, poll, peek).

Declaration of Java Queue Interface

The Queue interface is declared as:

public interface Queue extends Collection

We cannot instantiate a Queue directly as it is an interface. Here, we can use a class like LinkedList or PriorityQueue that implements this interface.

Queue<Obj> queue = new LinkedList<Obj>();

Now let us go through a simple example first, then we will deep dive into the article.

Example: Basic Queue using LinkedList


Output
Queue elements: []

Being an interface the queue needs a concrete class for the declaration and the most common classes are the PriorityQueue and LinkedList in Java. Note that neither of these implementations is thread-safe. PriorityBlockingQueue is one alternative implementation if the thread-safe implementation is needed.

Creating Queue Objects

Queue is an interface, so objects cannot be created of the type queue. We always need a class which extends this list in order to create an object. And also, after the introduction of Generics in Java 1.5, it is possible to restrict the type of object that can be stored in the Queue. This type-safe queue can be defined as:

Common Methods

The Queue interface provides several methods for adding, removing, and inspecting elements in the queue. Here are some of the most commonly used methods:

  • add(element): Adds an element to the rear of the queue. If the queue is full, it throws an exception.
  • offer(element): Adds an element to the rear of the queue. If the queue is full, it returns false.
  • remove(): Removes and returns the element at the front of the queue. If the queue is empty, it throws an exception.
  • poll(): Removes and returns the element at the front of the queue. If the queue is empty, it returns null.
  • element(): Returns the element at the front of the queue without removing it. If the queue is empty, it throws an exception.
  • peek(): Returns the element at the front of the queue without removing it. If the queue is empty, it returns null.

Example 1: This example demonstratesbasic queue operations.


Output
Queue: [apple, banana, cherry]
Removed element: apple
Queue after removal: [banana, cherry]
Peeked element: banana
Queue after peek: [banana, cherry, date]

Example 2:


Output
Elements of queue: [0, 1, 2, 3, 4]
Removed element:0
[1, 2, 3, 4]
Head of queue:1
Size of queue:4

Classes that implement the Queue Interface

1. PriorityQueue

PriorityQueue class lets us process elements based on their priority instead of the usual FIFO order of a normal queue. It’s useful when elements must be handled in priority order. Here’s how we can create a queue using this class.

Example:


Output
10
10
15

2. LinkedList

LinkedList is a linear data structure where elements are stored as separate objects, each containing data and a link to the next element. The elements are connected using pointers, not stored in continuous memory. Here’s how we can create a queue using this class.

Example:


Output
10
10
20

3. PriorityBlockingQueue

PriorityBlockingQueue is a thread-safe, unbounded blocking queue that orders elements like PriorityQueue and supports blocking retrieval. Since it’s unbounded, adding elements can still fail if memory runs out. Here’s how to create a queue using this class.

Example:


Output
10
10
15

Different Operations on Queue Interface using PriorityQueue class

1. Adding Elements

To add an element in a queue, we can use the add() method. The insertion order is not retained in the PriorityQueue. The elements are stored based on the priority order which is ascending by default. 

Example:


Output
[For, Geeks, Geeks]

2. Removing Elements

To remove an element from a queue, we can use the remove() method. If there are multiple objects, then the first occurrence of the object is removed. The poll() method is also used to remove the head and return it. 

Example:


Output
Initial Queue: [For, Geeks, Geeks]
After Remove: [For, Geeks]
Poll Method: For
Final Queue: [Geeks]

3. Iterating the Queue

There are multiple ways to iterate through the Queue. The most famous way is converting the queue to the array and traversing using the for loop. The queue has also an inbuilt iterator which can be used to iterate through the queue. 

Example:


Output
For Geeks Geeks 

Methods of Queue Interface

Here’s the full method list for the Queue<E> interface in Java, along with all methods it inherits from Collection<E> and Iterable<E>.

Method

Description

boolean add(E e)Inserts element; throws exception if full.
boolean offer(E e)Inserts element; returns false if full.
E remove()Removes head; throws exception if empty.
E poll()Removes head; returns null if empty.
E element()Retrieves head; throws exception if empty.
E peek()Retrieves head; returns null if empty.
boolean addAll(Collection<? extends E> c)Adds all elements from another collection.
void clear()Removes all elements.
boolean contains(Object o)Checks if element exists.
boolean containsAll(Collection<?> c)Checks if all elements exist.
boolean equals(Object o)Compares with another collection.
int hashCode()Returns hash code.
boolean isEmpty()Checks if collection is empty.
Iterator<E> iterator()Returns iterator for elements.
boolean remove(Object o)Removes a specific element.
boolean removeAll(Collection<?> c)Removes all matching elements.
boolean retainAll(Collection<?> c)Keeps only specified elements.
int size()Returns number of elements.
Object[] toArray()Returns elements as an array.
<T> T[] toArray(T[] a)Returns elements as a typed array.
default void forEach(Consumer<? super E> action)Performs action for each element.
default void forEach(Consumer<? super E> action)Performs action for each element.
default Spliterator<E> spliterator()Returns a spliterator.
default Stream<E> stream()Returns a sequential stream.
default Stream<E> parallelStream()Returns a parallel stream.

Queue in Java
Visit Course explore course icon

Similar Reads