Table of contents
1.
Introduction
2.
What is Queue Data Structure in Java?
3.
Types of Queues in Data Structure
3.1.
Simple Queue
3.1.1.
Working Mechanism
3.1.2.
Real-World Applications
3.2.
Circular Queue
3.2.1.
Working Mechanism
3.2.2.
Real-World Applications
3.3.
Priority Queue
3.3.1.
Working Mechanism
3.3.2.
Real-World Applications
3.4.
Double-Ended Queue (Deque)
3.4.1.
Working Mechanism:
3.4.2.
Real-World Applications:
4.
Queue Basic Functions
5.
Working of Queue
6.
Implementation of Queue in Java
6.1.
Using arrays
6.2.
Using Queue Interface
6.2.1.
Priority Queue Interface
6.2.2.
LinkedList Interface
7.
Application of Queue
8.
Advantages of Using Queue Data Structure in Java
9.
Limitations of Using Queue Data Structure in Java 
10.
Frequently Asked Questions
10.1.
Where can you find a queue in real life?
10.2.
What is the distinction between a stack and a queue?
10.3.
How many types of queues are there?
11.
Conclusion
Last Updated: Apr 13, 2025
Easy

Queue Data Structure and Implementation in Java

Author Aditi
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Imagine you're waiting in a line to buy movie tickets — the person who arrives first gets served first. This is exactly how a queue works in programming! It follows the First In, First Out (FIFO) principle. 

Queue Data Structure and Implementation in Java

In this article, we’ll learn how queues function, how to implement them in Java, and where they’re used in real life, such as in printers, scheduling, and more.

What is Queue Data Structure in Java?

A queue is a linear data structure made up of a collection of objects that are arranged in a first-in-first-out order. This means that the first thing you put in will be the first thing you take out. Items are also removed in the order in which they were introduced.

Using a real-world scenario, we can relate a queue data structure to a line of people waiting for service. When one person is served, they go to the front of the line to serve the next person. They are assisted in the order that they arrive. Enqueue is the programming term for adding things to a queue, and dequeue is the term for removing items from a queue.

Types of Queues in Data Structure

Simple Queue

A Simple Queue, also known as a linear queue, follows the First In, First Out (FIFO) rule. The first element inserted is the first one to be removed. Elements are added at the rear and removed from the front.

Working Mechanism

  • Enqueue operation adds elements to the rear.
     
  • Dequeue operation removes elements from the front.
     
  • Once an element is removed, that space isn’t reused unless reset manually.

Real-World Applications

  • Ticket counters where people stand in line and get served one by one.
     
  • CPU task scheduling, where processes are executed in the order they arrive.

Circular Queue

A Circular Queue overcomes the limitation of a simple queue by making the last position connected back to the first, forming a circle. It also follows the FIFO rule but allows space reuse.

Working Mechanism

  • Enqueue and dequeue operations are performed in a circular manner.
     
  • Rear and front pointers wrap around when they reach the end of the array.

Real-World Applications

  • Memory management in operating systems, like buffering.
     
  • Traffic light systems where each signal takes turns in a cyclic order.

Priority Queue

A Priority Queue is a special type where each element is assigned a priority. Elements with higher priority are dequeued first, not necessarily the one that came first.

Working Mechanism

  • Elements are added in any order.
     
  • The element with the highest priority is served first.
     
  • If priorities are the same, it follows FIFO.

Real-World Applications

  • Hospital emergency rooms, where critical patients are treated before others.
     
  • Job scheduling in operating systems, where important tasks are prioritized.

Double-Ended Queue (Deque)

A Deque (pronounced “deck”) or Double-Ended Queue allows insertion and deletion at both front and rear ends. It supports more flexibility than a regular queue.

Working Mechanism:

  • It can function as both a queue (FIFO) and a stack (LIFO).
     
  • Supports operations like insertFront, insertRear, deleteFront, and deleteRear.

Real-World Applications:

  • Undo/redo operations in software, where actions can be added or removed from either end.
     
  • Palindrome checking, as it allows comparison from both ends of a word or phrase.

Queue Basic Functions

The following are some basic functions defined for a queue.

FunctionDescription
enqueue()Adds an element to the rear (back) of the queue.
dequeue()Removes an element from the front of the queue.
isEmpty()Checks whether the queue has no elements. Returns true if empty.
isFull()Checks whether the queue has reached its maximum capacity.
peek()Retrieves the front element without removing it from the queue.
Time ComplexityBoth enqueue() and dequeue() operations using an array have O(1) time complexity.

Enqueue and dequeue operations in a queue utilizing an array have a complexity O(1).

Working of Queue

The following is how queue operations work:

  • There are two pointers REAR AND FRONT
  • FRONT keeps track of the queue's first item.
  • REAR will monitor the queue's final element, 
  • FRONT and REAR are set to -1 initially.

 

Operation Enqueue

  • Check whether the queue is full.
  • Set the value of FRONT to 0 for the first element.
  • REAR index should be increased by one.
  • REAR points to a place where the new element should be added.
     

Operation Dequeue

  • Check whether the queue isn't full.
  • FRONT points to a value that is to be returned.
  • Rise 1 in the FRONT index
  • Set the values of FRONT and REAR to -1 for the final element.

Implementation of Queue in Java

Since we have known about the basic functions of queue and working of queue data structure. Now, it's time to see how to implement a queue using program source code in the Java programming language.

Using arrays

Queue can be implemented using arrays. But the main disadvantage is that the size of the queue will be fixed. Source code is given below displaying basic functions of queue:-

Code
class Queue {
 
    int len = 5;
    int items[] = new int[len];
    int front = -1;
    int rear = -1;
   
    boolean isFull(){
        if(rear == len - 1){
            return true;
        } else {
            return false;
        }
    }
   
    boolean isEmpty(){
        if(front == -1 && rear == -1){
            return true;
        } else {
            return false;
        }
    }
   
    void enQueue(int itemValue) {
        if(isFull()){
            System.out.println("Queue is full, Overflow condition");
        } else if(front == -1 && rear == -1){
            front = rear = 0;
            items[rear] = itemValue;
        } else{
            rear++;
            items[rear] = itemValue;
        }
    }
   
    void deQueue(){
        if(isEmpty()){
            System.out.println("Queue is empty. Nothing to dequeue. Underflow condition!!");
        } else if (front == rear){
            front = rear = -1;
        } else {
            front++;
        }
    }
   
    void display(){
        int i;
       
        if(isEmpty()){
            System.out.println("Queue is empty, underflow condition!!");
        } else {
            for(i = front; i <= rear; i++){
                System.out.println(items[i]);
            }
        }
    }
   
    void peak(){
        System.out.println("Front index value is: " + items[front]);
    }
   
  }

  public class Main
  {
    public static void main(String[] args) {
        Queue myQueue = new Queue();
       
        myQueue.enQueue(10);
        myQueue.enQueue(20);
        myQueue.enQueue(30);
        myQueue.enQueue(40);
        myQueue.enQueue(50);
        
        myQueue.display();
        myQueue.peak();
        
        System.out.println("After Dequeue Operation:-");
        myQueue.deQueue();
        myQueue.peak();
        myQueue.display();       
    }
}
You can also try this code with Online Java Compiler
Run Code

Output:-

10
  20
  30
  40
  50
  Front index value is: 10
  After Dequeue Operation:-
  Front index value is: 20
  20
  30
  40
  50
You can also try this code with Online Java Compiler
Run Code

Using Queue Interface

The Queue interface, which is part of Java Collections, has two implementations:

1. Queue queue = new LinkedList();
2. Queue queue = new PriorityQueue();
You can also try this code with Online Java Compiler
Run Code

The Queue interface is implemented by two classes: LinkedList and PriorityQueue. We can't make an instance of the Queue because it's an interface. As a result, we create a LinkedList and PriorityQueue instance and attach it to the queue interface.

add(): This method adds a particular element to the queue's end. Because the return type is boolean, it returns true if the element is successfully added and false otherwise.

element(): This method returns the queue's initial entry.

remove(): This function removes the queue's first entry.

poll(): This function is similar to remove(), except if the queue is empty, the poll returns null.

peek() is similar to element(), but the only difference is that element returns null if the queue is empty, whereas the peek method returns null.

Priority Queue Interface

import java.util.*;
public class Main
{
   public static void main(String[] args)
   {
      Queue<Integer> queue = new PriorityQueue<Integer>();
       
      //Adding elements to the Queue
      queue.add(10);
      queue.add(20);
      queue.add(30);
      queue.add(40);
      queue.add(50);
 
      System.out.println("Elements in Queue: "+ queue);

      System.out.println("Removed element: "+ queue.remove());

      System.out.println("Front: "+ queue.element());
       
      System.out.println("poll(): "+ queue.poll());

      System.out.println("peek(): "+ queue.peek());

      System.out.println("Add new element: "+ queue.add(60));

      System.out.println("Elements in Queue: " + queue);
   }
}
You can also try this code with Online Java Compiler
Run Code

Output

Elements in Queue: [10, 20, 30, 40, 50]
Removed element: 10
Front: 20
poll(): 20
peek(): 30
Add new element: true
Elements in Queue: [30, 40, 50, 60]
You can also try this code with Online Java Compiler
Run Code

LinkedList Interface

import java.util.*;

public class Main
{

  public static void main (String[]args)
  {

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

    //Adding elements to the Queue
    queue.add (10);
    queue.add (20);
    queue.add (30);
    queue.add (40);
    queue.add (50);

    System.out.println ("Elements in Queue:" + queue);

    System.out.println ("Removed element: " + queue.remove ());

    System.out.println ("Front: " + queue.element ());

    System.out.println ("poll(): " + queue.poll ());

    System.out.println ("peek(): " + queue.peek ());

    System.out.println ("Add new element: " + queue.add (60));

    System.out.println ("Elements in Queue: " + queue);
  }
}
You can also try this code with Online Java Compiler
Run Code

Output

Elements in Queue:[10, 20, 30, 40, 50]
Removed element: 10
Front: 20
poll(): 20
peek(): 30
Add new element: true
Elements in Queue: [30, 40, 50, 60]
You can also try this code with Online Java Compiler
Run Code

Application of Queue

The following are some of the applications of a queue.

  • Scheduling of CPUs and Disks
  • Interrupt handling in real-time systems.
  • Queues are used in call center phone systems to keep people calling in the order.
  • A queue is used in network equipment such as routers and switches. A mail queue, which is a directory that holds data and controls files for mail messages, is another application of a queue.
  • When numerous programs are running in the main memory, this is called multi-programming. These programs must be arranged, and these multiple programs are organized as queues.

Advantages of Using Queue Data Structure in Java

  • Built-in Support in Collections Framework
    Java provides a rich Queue interface and classes like LinkedList, PriorityQueue, and ArrayDeque, making queue implementation easy without writing code from scratch.
     
  • Ease of Implementation
    Queues in Java can be easily implemented using built-in classes. For instance, LinkedList allows quick setup with standard methods like offer(), poll(), and peek().
     
  • Supports Multiple Queue Types
    Java supports various queue types—simple, priority, double-ended—through interfaces and specialized classes, allowing developers to choose the right type for their use case.
     
  • Thread-Safe Variants Available
    Java provides thread-safe queues like ConcurrentLinkedQueue and BlockingQueue, which are useful for multithreading and concurrent data processing.
     
  • FIFO Order is Maintained Automatically
    The queue structure in Java ensures first-in-first-out behavior without needing custom code, making it ideal for managing tasks, buffers, and resource scheduling.
     
  • Improves Application Design
    Using queues helps structure asynchronous tasks and background processing, improving the scalability and responsiveness of Java applications like web servers or message systems.
     
  • Better Resource Handling
    Java queues can be used to manage limited resources like printers or threads efficiently, reducing race conditions and ensuring orderly execution.

Limitations of Using Queue Data Structure in Java 

There are disadvantages of using Queue data structures and they are as follows:-

  • Insertion and deletion of components from the middle are time-consuming processes.
     
  • A new element can only be added to a traditional queue once the previous elements have been removed.
     
  • It takes O(N) time to search for an element.
     
  • A queue's maximum size must be determined beforehand when implemented using array.

Frequently Asked Questions

Where can you find a queue in real life?

In the real world, here are some examples of queues: a ticket line, an escalator, and a vehicle wash.

What is the distinction between a stack and a queue?

The main distinction between Stack and Queue Data Structures is that Stack uses a LIFO(Last In First Out) data structure whereas Queue uses a FIFO(First In First Out) data structure. 

How many types of queues are there?

There are mainly four types: Simple Queue, Circular Queue, Priority Queue and De-queue(Double-ended queue).

Conclusion

In this article, we have extensively defined a queue and its structure in the Java programming language. Finally, we looked at using arrays and queue interfaces in Java to construct the queue data structure.

Live masterclass