A queue is a way of storing items in a line. New items join at the back, called the rear, and items leave from the front, called the front. The first item that joined is the first one to leave. This rule is called FIFO: First In, First Out.
This page uses a ticket counter line to explain the idea, then walks through enqueue, dequeue and peek, and ends with a small Java program you can run. If you have already read our stack data structure guide, a queue is its opposite.
Queue in simple words: the ticket-line analogy
Picture the line at a railway ticket counter or a cinema box office.
- A new person always joins at the back of the line.
- The person at the front is served and leaves.
- Nobody jumps the line or leaves from the middle.
Whoever came first is served first. That is exactly how a queue works.
| At the ticket counter | In a queue |
|---|---|
| A person joins at the back | enqueue |
| The person at the front is served and leaves | dequeue |
| Seeing who is next without serving them | peek |
| Nobody waiting | The queue is empty |
| The hall cannot hold more people | The queue is full (only for fixed-size queues) |
FIFO explained with a picture
Here is a queue after adding 101, then 102, then 103:
front rear
| |
v v
+-----+-----+-----+
| 101 | 102 | 103 |
+-----+-----+-----+
leaves here joins here
101 joined first, so it leaves first. 103 joined last, so it waits until 101 and 102 have left.
The opposite rule is LIFO (Last In, First Out), used by a stack. See stack vs queue for a full side-by-side comparison.
Queue operations: enqueue, dequeue and peek
| Operation | What it does | Time |
|---|---|---|
enqueue(x) |
Adds x at the rear |
O(1) |
dequeue() |
Removes the front item and returns it | O(1) |
peek() |
Returns the front item without removing it | O(1) |
isEmpty() |
Tells you if the queue has no items | O(1) |
size() |
Number of items | O(1) |
Enqueue and dequeue step by step
Start: (empty)
enqueue(101): [101] front = 101
enqueue(102): [101, 102] front = 101
enqueue(103): [101, 102, 103] front = 101
dequeue(): [102, 103] returns 101
peek(): [102, 103] returns 102, nothing removed
enqueue(104): [102, 103, 104]
dequeue(): [103, 104] returns 102
Read the lists left to right as front to rear.
Why a simple array queue wastes space
Suppose you build a queue on an array of size 5 and keep two indexes: front and rear. Each dequeue moves front forward. After a few dequeues, the slots at the start of the array are empty, but rear has already reached the end:
index: 0 1 2 3 4
[ - | - | 103 | 104 | 105 ]
^ ^
front rear
There is room for two more items, but a simple array queue would say it is full. Shifting every item left after each dequeue fixes this, but that makes dequeue O(n).
The better fix is a circular queue. When rear reaches the last index, it wraps around to index 0. The formula is:
rear = (rear + 1) % capacity
The same formula moves front. Now every slot is reused and every operation stays O(1).
A small circular queue in Java
This program builds a circular queue on an array of size 5. It keeps a size counter so it can tell "empty" from "full".
public class QueueDemo {
static int[] items = new int[5];
static int front = 0;
static int rear = -1;
static int size = 0;
static void enqueue(int value) {
if (size == items.length) {
System.out.println("Queue is full, cannot add " + value);
return;
}
rear = (rear + 1) % items.length;
items[rear] = value;
size++;
}
static int dequeue() {
if (size == 0) {
System.out.println("Queue is empty");
return -1;
}
int value = items[front];
front = (front + 1) % items.length;
size--;
return value;
}
static int peek() {
if (size == 0) {
System.out.println("Queue is empty");
return -1;
}
return items[front];
}
public static void main(String[] args) {
enqueue(101);
enqueue(102);
enqueue(103);
System.out.println("Front: " + peek());
System.out.println("Served: " + dequeue());
System.out.println("Served: " + dequeue());
enqueue(104);
enqueue(105);
enqueue(106);
enqueue(107);
enqueue(108);
System.out.println("Front now: " + peek() + ", people waiting: " + size);
}
}
Output:
Front: 101
Served: 101
Served: 102
Queue is full, cannot add 108
Front now: 103, people waiting: 5
Save it as QueueDemo.java and run java QueueDemo.java with Java 11 or later.
What happened:
- 101, 102 and 103 go into indexes 0, 1 and 2.
- Two dequeues serve 101 and 102.
frontmoves to index 2. - 104 and 105 go into indexes 3 and 4. Then
rearwraps around, so 106 goes into index 0 and 107 into index 1. - The queue now holds 5 items, so 108 is refused.
- The front is still 103, because it has waited the longest.
Common mistake: Checking "full" with
rear == items.length - 1. In a circular queue,rearcan be at the last index while there is free space at the start. Keep asizecounter, as above, and compare it with the capacity.
A queue can also be built on a linked list with a head and a tail reference: enqueue adds after the tail, dequeue removes the head. There is then no fixed capacity.
Using a queue from the Java library
In real Java code, you use the Queue interface with ArrayDeque (or LinkedList):
import java.util.ArrayDeque;
import java.util.Queue;
public class BuiltInQueue {
public static void main(String[] args) {
Queue<String> line = new ArrayDeque<>();
line.offer("Ravi");
line.offer("Priya");
line.offer("Kiran");
System.out.println(line);
System.out.println("Next: " + line.peek());
System.out.println("Served: " + line.poll());
System.out.println(line + " size " + line.size());
line.poll();
line.poll();
System.out.println("Empty poll gives: " + line.poll());
}
}
Output:
[Ravi, Priya, Kiran]
Next: Ravi
Served: Ravi
[Priya, Kiran] size 2
Empty poll gives: null
| Action | Throws an exception if it fails | Returns a special value |
|---|---|---|
| Add at rear | add(x) |
offer(x) returns false |
| Remove front | remove() |
poll() returns null |
| Look at front | element() |
peek() returns null |
Beginners usually use offer, poll and peek, and check for null.
Types of queues
| Type | How it works | Example use |
|---|---|---|
| Simple queue | Add at rear, remove at front | Ticket line |
| Circular queue | Array queue where indexes wrap around | Fixed-size buffers, such as keyboard input |
| Priority queue | The item with the highest priority leaves first, not the oldest | Hospital emergency room, task schedulers; Java's PriorityQueue |
| Deque (double-ended queue) | Add and remove at both ends | Java's ArrayDeque, which can act as a stack or a queue |
Where queues are used
- Printers: documents print in the order they were sent.
- Operating systems: processes waiting for the CPU sit in queues. Round-robin scheduling gives each one a turn in order.
- Web servers: incoming requests wait in a queue when the server is busy.
- Messaging systems such as Kafka and RabbitMQ pass messages between services in order.
- Breadth-first search (BFS) in graphs and trees visits nodes level by level using a queue.
- Call centres and food delivery apps hold waiting customers or orders in a queue.
Queue vs stack in one table
| Point | Queue | Stack |
|---|---|---|
| Rule | FIFO, first in first out | LIFO, last in first out |
| Add | enqueue, at the rear | push, at the top |
| Remove | dequeue, from the front | pop, from the top |
| Everyday example | Ticket counter line | Pile of plates |
| Graph search that uses it | BFS | DFS |
Read stack vs queue for more differences and interview questions on both.
Practice problems for beginners
- Reverse the first k items of a queue.
- Build a queue using two stacks.
- Generate binary numbers from 1 to n using a queue (1, 10, 11, 100, ...).
- Find the first non-repeating character in a stream of letters.
- Print a binary tree level by level.
Quick revision
- A queue adds at the rear and removes from the front.
- It follows FIFO: first in, first out.
- enqueue adds, dequeue removes, peek looks. All are O(1) in a circular or linked queue.
- A circular queue reuses array slots with
(index + 1) % capacity. - In Java, use
Queue<T> q = new ArrayDeque<>()withoffer,pollandpeek.
Next, compare it again with the stack data structure, then continue with our data structures and algorithms guide and the DSA interview questions.
Frequently Asked Questions
What is a queue in simple words?
What does FIFO mean?
What are enqueue and dequeue?
What is a circular queue?
Which class should I use for a queue in Java?
What is the difference between a queue and a stack?
Want to Build Your Career in Java Full Stack with AI?
Join CodeBegun and train with working industry engineers — Check the Java Full Stack training details

