Ugenz Academy/Tutorial

Queue

5 min read924 words

Reading progress

Keep learning

Queue in Java

Introduction

A Queue in Java is a collection that is used to store elements in a particular order. It generally follows the FIFO (First In, First Out) principle.

This means the element added first is usually the element removed first.

The Queue interface is part of the Java Collection Framework and is available in the java.util package.

Example

Queue ---------------- A → B → C → D First element added: A First element removed: A

Features of Queue

  • Generally follows FIFO order.
  • Elements are added at the rear.
  • Elements are removed from the front.
  • Supports insertion and removal operations.
  • Allows efficient processing of elements.
  • Useful for scheduling and waiting-line applications.

Queue Syntax

Queue<Type> queueName = new LinkedList<>();

Example:

Queue<String> students = new LinkedList<>();

Example of Queue

import java.util.*; public class QueueExample { public static void main(String[] args) { Queue<String> queue = new LinkedList<>(); queue.add("Anu"); queue.add("Ram"); queue.add("Sita"); System.out.println(queue); System.out.println(queue.poll()); System.out.println(queue); } }

Output

[Anu, Ram, Sita] Anu [Ram, Sita]

The first element "Anu" was removed first because Queue generally follows FIFO.

Common Queue Methods

MethodDescription
add()Adds an element
offer()Adds an element
remove()Removes and returns the front element
poll()Removes and returns the front element
element()Returns the front element
peek()Returns the front element without removing it
size()Returns the number of elements
isEmpty()Checks whether the Queue is empty

Example of Queue Methods

import java.util.*; public class QueueMethods { public static void main(String[] args) { Queue<Integer> numbers = new LinkedList<>(); numbers.offer(10); numbers.offer(20); numbers.offer(30); System.out.println(numbers); System.out.println(numbers.peek()); System.out.println(numbers.poll()); System.out.println(numbers); } }

Output

[10, 20, 30] 10 10 [20, 30]

Difference Between add() and offer()

Both methods are used to insert elements into a Queue.

add()offer()
Adds an elementAdds an element
May throw an exception if insertion failsReturns false if insertion fails
Part of Collection interfaceSpecifically useful for Queue operations

Difference Between remove() and poll()

Both methods remove the front element.

remove()poll()
Removes the front elementRemoves the front element
Throws exception if Queue is emptyReturns null if Queue is empty

Difference Between element() and peek()

Both methods return the front element without removing it.

element()peek()
Returns the front elementReturns the front element
Throws exception if Queue is emptyReturns null if Queue is empty

Types of Queue

1. LinkedList

LinkedList can be used as a Queue.

Queue<String> queue = new LinkedList<>(); queue.add("A"); queue.add("B"); queue.add("C"); System.out.println(queue);

2. PriorityQueue

PriorityQueue processes elements according to their priority rather than simple insertion order.

Queue<Integer> numbers = new PriorityQueue<>(); numbers.add(30); numbers.add(10); numbers.add(20); System.out.println(numbers);

The smallest element has the highest priority by default.

For example, removing elements:

System.out.println(numbers.poll()); System.out.println(numbers.poll());

Output:

10 20

3. ArrayDeque

ArrayDeque is a resizable-array implementation of the Deque interface and can be used as a Queue.

Queue<String> queue = new ArrayDeque<>(); queue.add("A"); queue.add("B"); queue.add("C"); System.out.println(queue);

Queue vs Stack

QueueStack
Generally follows FIFOFollows LIFO
First element is removed firstLast element is removed first
Insertion at rearInsertion at top
Removal from frontRemoval from top
Used in schedulingUsed in undo operations

Queue vs List

QueueList
Designed for processing elements in queue orderDesigned for general ordered storage
Usually uses front/rear operationsUses index-based operations
poll(), offer(), peek() are commonget(), set(), add() are common
Useful for waiting-line systemsUseful for general collections

Real-Life Examples

Queue is commonly used in situations where the first person or item should be processed first.

Examples:

  • Printer job scheduling
  • Customer service lines
  • CPU task scheduling
  • Ticket booking systems
  • Network data processing
  • Call center waiting systems

Real-Life Example

Customer Queue Anu → Ram → Sita → Hari ↑ First customer Anu is served first.

Advantages of Queue

  1. Follows FIFO processing in typical Queue implementations.
  2. Useful for scheduling tasks.
  3. Provides efficient insertion and removal operations.
  4. Useful for managing waiting lines.
  5. Supports different implementations such as LinkedList, PriorityQueue, and ArrayDeque.

Key Points

  • Queue is an interface in Java.
  • It belongs to the java.util package.
  • Queue generally follows FIFO.
  • offer() and add() are used for insertion.
  • poll() and remove() are used for removal.
  • peek() returns the front element without removing it.
  • PriorityQueue processes elements according to priority.
  • ArrayDeque can also be used as a Queue.

Conclusion

A Queue is an important Java collection used when elements need to be processed in a specific order. It is commonly based on the FIFO principle, making it useful for scheduling, task processing, and real-world waiting-line systems.

Keep learning. Keep building. Keep growing.

© 2026 Ugenz Academy