Java Queue

Last Updated : 24 Aug 2026

A queue is a kind of linear data structure that is used to store elements in a particular manner. It stores the same type of elements. The elements in a queue are stored in a FIFO (First In, First Out) order. There are two ends in the queue i.e., front and rear.

The following figure describes the FIFO (First In, First Out) property of the Java queue.

Java Queue

We observe that the queue has two terminals, i.e., start (front) and end (rear). Elements are added inside the queue from the rear end and removed from the front end.

Java Queue Interface

The Queue is an interface in the Java that belongs to Java.util package. It also extends the Collection interface.

The generic representation of the Java Queue interface is as follows:

public interface Queue<E> extends Collection<E>

Where, E is the type of elements held in this collection.

Note that we cannot create an instance of the interfaces. If we want to implement the functionality of the Queue interface, it is mandatory to have some solid classes that implement the Queue interface.

In Java, there are the following two classes that are used to implement the Queue interface.

Java Queue

Characteristics of the Java Queue

The significant properties of the Java Queue data structure are given as follows:

  • The Java Queue interface gives all the rules and processes of the Collection interface like inclusion, deletion, etc.
  • Other than these two classes, the ArrayBlockingQueue also implement the Queue interface.
  • There are two types of queues, Unbounded queues and Bounded queues. The Queues that are a part of the java.util package are known as the Unbounded queues and bounded queues are the queues that are present in java.util.concurrent package.
  • The Deque or (double-ended queue) is also a type of queue that carries the inclusion and deletion of elements from both ends.
  • The deque is also considered thread-safe.
  • BlockingQueue is also a type of queue that is thread-safe. The BlockingQueue is used to implement the producer-consumer queries.
  • BlockingQueue does not support null elements. If we try to insert null values, it throws NullPointerException.

Implementation of Queue

Java provides the following classes to implement the Queue:

Interfaces Used in Implementation of Queue

The Java interfaces are also used in the implementation of the Java queue. The interfaces that are used to implement the functionalities of the queue are given as follows:

Java Queue
  • Deque
  • Blocking Queue
  • Blocking Deque
Java Queue

Java Queue Class Methods

In the Java queue, there are many methods that are used very commonly. The Queue interface promotes different methods like insert, delete, peek, etc. Some of the operations of the Java queue raise an exception whereas some of these operations return a particular value when the program is completed.

Note: In Java SE 8, there are no changes made in the Java queue collection. These methods which are defined under are further prepared in the succeeding versions of the Java programming language. For example, Java SE 9.

MethodDescription
boolean add(E e)Adds element e to the queue at the end (tail) of the queue without violating the restrictions on the capacity. Returns true if success or IllegalStateException if the capacity is exhausted.
E peek()Returns the head (front) of the queue without removing it.
E element()Performs the same operation as the peek () method. Throws NoSuchElementException when the queue is empty.
E remove()Removes the head of the queue and returns it. Throws NoSuchElementException if queue is empty.
E poll()Removes the head of the queue and returns it. If the queue is empty, it returns null.
boolean offer(E e)Insert the new element e into the queue without violating capacity restrictions.
int size()Returns the size or number of elements in the queue.

Queue Implementation

There are the following two ways to implement queue in Java:

  1. Using Array
  2. Using LinkedList

Implementation of Queue Using Array

Queue implementation is not as straightforward as a stack implementation.

To implement queue using Arrays, we first declare an array that holds n number of elements.

Then we define the following operations to be performed in this queue.

1) Enqueue: An operation to insert an element in the queue is Enqueue (function queue Enqueue in the program). For inserting an element at the rear end, we need first to check if the queue is full. If it is full, then we cannot insert the element. If rear < n, then we insert the element in the queue.

2) Dequeue: The operation to delete an element from the queue is Dequeue (function queue Dequeue in the program). First, we check whether the queue is empty. For dequeue operation to work, there has to be at least one element in the queue.

3) Front: It returns the front of the queue.

4) Display: It traverses the queue and displays the elements of the queue.

Example: Implementation of Queue Using Array

Java

class Queue {   
    private static int front, rear, capacity;   
    private static int queue[];   
    Queue(int size) {   
        front = rear = 0;   
        capacity = size;   
        queue = new int[capacity];   
    }   
    // insert an element into the queue  
    static void queueEnqueue(int item) {   
        // check if the queue is full  
        if (capacity == rear) {   
            System.out.printf("\nQueue is full\n");   
            return;   
        }   
        // insert element at the rear   
        else {   
            queue[rear] = item;   
            rear++;   
        }   
        return;   
    }   
    //remove an element from the queue  
    static void queueDequeue() {   
        // check if queue is empty   
        if (front == rear) {   
            System.out.printf("\nQueue is empty\n");   
            return;   
        }   
        // shift elements to the right by one place uptil rear   
        else {   
            for (int i = 0; i < rear - 1; i++) {   
                queue[i] = queue[i + 1];   
            }   
      // set queue[rear] to 0  
            if (rear < capacity)   
                queue[rear] = 0;   
            // decrement rear   
            rear--;   
        }   
        return;   
    }   
    // print queue elements   
    static void queueDisplay() {   
        int i;   
        if (front == rear) {   
            System.out.printf("Queue is Empty\n");   
            return;   
        }   
        // traverse front to rear and print elements   
        for (i = front; i < rear; i++) {   
            System.out.printf(" %d, ", queue[i]);   
        }   
        return;   
    }   
    // print front of queue   
    static void queueFront() {   
        if (front == rear) {   
            System.out.printf("Queue is Empty\n");   
            return;   
        }   
        System.out.printf("\nFront Element of the queue: %d", queue[front]);   
        return;   
    }   
}   
public class Main {  
    public static void main(String[] args) {   
        // Create a queue of capacity 4   
        Queue q = new Queue(4);   
        System.out.println("Initial Queue:");  
       // print Queue elements   
        q.queueDisplay();   
        // inserting elements in the queue   
        q.queueEnqueue(10);   
        q.queueEnqueue(30);   
        q.queueEnqueue(50);   
        q.queueEnqueue(70);   
        // print Queue elements   
        System.out.println("Queue after Enqueue Operation:");  
        q.queueDisplay();   
        // print front of the queue   
        q.queueFront();   
        // insert element in the queue   
        q.queueEnqueue(90);   
        // print Queue elements   
        q.queueDisplay();   
        q.queueDequeue();   
        q.queueDequeue();   
        System.out.printf("\nQueue after two dequeue operations:");   
        // print Queue elements   
        q.queueDisplay();   
        // print front of the queue   
        q.queueFront();   
    }   
}  
Compile and Run

Output:

Initial Queue:
Queue is Empty
Queue after Enqueue Operation:
10, 30, 50, 70,
Front Element of the queue: 10
Queue is full
10, 30, 50, 70,
Queue after two dequeue operations: 50, 70,
Front Element of the queue: 50

Implementation of Queue Using LinkedList

In this program we will implement queue using LinkedList and perform the enqueue(), dequeue(), front(), and display() operations.

Example: Implementing Queue Using LinkedList

Java

class LinkedListQueue  {  
 private Node front, rear;  
 private int queueSize; // queue size  
 //linked list node  
 private class Node {  
 int data;  
 Node next;  
 }  
 //default constructor - initially front & rear are null; size=0; queue is empty  
 public LinkedListQueue()   {  
 front = null;  
 rear = null;  
 queueSize = 0;  
 }  
//check if the queue is empty  
 public boolean isEmpty()   {  
 return (queueSize == 0);  
 }    
 //Remove item from the front of the queue.  
 public int dequeue()  {  
 int data = front.data;  
 front = front.next;  
 if (isEmpty())   {  
 rear = null;  
 }  
 queueSize--;  
 System.out.println("Element " + data+ " removed from the queue");  
 return data;  
 }  
 //Add data at the rear of the queue.  
 public void enqueue(int data)   {  
 Node oldRear = rear;  
 rear = new Node();  
 rear.data = data;  
 rear.next = null;  
 if (isEmpty())   {  
 front = rear;  
 }  
 else {  
 oldRear.next = rear;  
 }  
 queueSize++;  
 System.out.println("Element " + data+ " added to the queue");  
 }  
 //print front and rear of the queue  
 public void print_frontRear() {  
System.out.println("Front of the queue: " + front.data  + " \nRear of the queue: " + rear.data);  
 }  
}  
public class Main {  
 public static void main(String a[]){  
 LinkedListQueue queue = new LinkedListQueue();  
 queue.enqueue(6);  
 queue.enqueue(3);  
 queue.print_frontRear();  
 queue.enqueue(12);  
 queue.enqueue(24);  
 queue.dequeue();  
 queue.dequeue();  
 queue.enqueue(9);   
 queue.print_frontRear();  
 }
}  
Compile and Run

Output:

Element 6 added to the queue
Element 3 added to the queue
Front of the queue: 6 
Rear of the queue: 3
Element 12 added to the queue
Element 24 added to the queue
Element 6 removed from the queue
Element 3 removed from the queue
Element 9 added to the queue
Front of the queue: 12 
Rear of the queue: 9