Java Queue
A Queue in Java is a linear data structure that stores and manages elements in a First In, First Out (FIFO) order. Elements are added to the rear of the queue and removed from the front, similar to how people wait in line at a store or bank. The queue follows the principle where the first element added is the first one to be removed.
Syntax
Since Queue is an interface in Java, it cannot be instantiated directly. Instead, use implementing classes to create queue objects:
Creating Queue Objects
Queue<DataType> queueName = new ImplementingClass<>();
Parameters:
DataType: The type of elements the queue will storequeueName: The name of the queue variableImplementingClass: A class that implements theQueueinterface (e.g.,LinkedList,ArrayDeque,PriorityQueue)
Return value:
Queue methods return different values based on success/failure scenarios as described above.
Common Methods in the Queue Interface
add(element): Inserts element at the rear of the queue, throws exception if it failsoffer(element): Inserts element at the rear of the queue, returns false if it failsremove(): Removes and returns the element from the front, throws an exception if emptypoll(): Removes and returns element from front, returns null if emptyelement(): Returns front element without removing, throws exception if emptypeek(): Returns front element without removing, returns null if empty
Example 1: Basic Operations for Queue in Java
This example demonstrates fundamental queue operations, including adding elements, removing elements, and accessing the front element:
import java.util.Queue;import java.util.LinkedList;public class BasicQueueExample {public static void main(String[] args) {// Create a queue using LinkedList implementationQueue<String> customerQueue = new LinkedList<>();// Add customers to the queuecustomerQueue.offer("Alice");customerQueue.offer("Bob");customerQueue.offer("Charlie");System.out.println("Queue: " + customerQueue);// Serve the first customer (remove from front)String servedCustomer = customerQueue.poll();System.out.println("Served customer: " + servedCustomer);// Check who is next without removing themString nextCustomer = customerQueue.peek();System.out.println("Next customer: " + nextCustomer);System.out.println("Updated queue: " + customerQueue);}}
The output of this code is:
Queue: [Alice, Bob, Charlie]Served customer: AliceNext customer: BobUpdated queue: [Bob, Charlie]
This example demonstrates how elements maintain their order in the queue. Alice, who was added first, is served first, maintaining the FIFO principle.
Example 2: Using Queue to Implement a Print Job Queue System
This example simulates a real-world scenario where a printer processes print jobs in the order they were submitted:
import java.util.Queue;import java.util.LinkedList;public class PrintJobQueue {public static void main(String[] args) {// Create a queue to manage print jobsQueue<String> printQueue = new LinkedList<>();// Submit print jobs to the queueprintQueue.add("Document1.pdf");printQueue.add("Presentation.pptx");printQueue.add("Report.docx");printQueue.add("Invoice.xlsx");System.out.println("Print jobs in queue: " + printQueue.size());// Process each print job in FIFO orderwhile (!printQueue.isEmpty()) {String currentJob = printQueue.remove();System.out.println("Printing: " + currentJob);// Simulate printing timetry {Thread.sleep(1000); // Wait 1 second} catch (InterruptedException e) {Thread.currentThread().interrupt();}}System.out.println("All print jobs completed!");System.out.println("Queue status: " + (printQueue.isEmpty() ? "Empty" : "Has jobs"));}}
The output of this code is:
Print jobs in queue: 4Printing: Document1.pdfPrinting: Presentation.pptxPrinting: Report.docxPrinting: Invoice.xlsxAll print jobs completed!Queue status: Empty
This example shows how queues ensure fair processing where jobs are handled in the order they arrive, preventing newer jobs from jumping ahead of older ones.
Example 3: Breadth-First Search Implementation Using Queue
This example demonstrates using a queue to implement breadth-first search in a tree structure, showcasing a common algorithmic use case:
import java.util.Queue;import java.util.LinkedList;import java.util.List;import java.util.ArrayList;class TreeNode {int val;TreeNode left;TreeNode right;TreeNode(int val) {this.val = val;}}public class BreadthFirstSearch {public static void main(String[] args) {// Create a binary treeTreeNode root = new TreeNode(1);root.left = new TreeNode(2);root.right = new TreeNode(3);root.left.left = new TreeNode(4);root.left.right = new TreeNode(5);root.right.left = new TreeNode(6);root.right.right = new TreeNode(7);List<Integer> result = breadthFirstTraversal(root);System.out.println("BFS traversal: " + result);}public static List<Integer> breadthFirstTraversal(TreeNode root) {List<Integer> result = new ArrayList<>();if (root == null) return result;// Queue to store nodes for level-by-level processingQueue<TreeNode> queue = new LinkedList<>();queue.offer(root);while (!queue.isEmpty()) {TreeNode current = queue.poll();result.add(current.val);// Add child nodes to queue for next level processingif (current.left != null) {queue.offer(current.left);}if (current.right != null) {queue.offer(current.right);}}return result;}}
The output of this code is:
BFS traversal: [1, 2, 3, 4, 5, 6, 7]
This example illustrates how queues enable level-by-level tree traversal, ensuring nodes are processed in breadth-first order rather than depth-first order.
Frequently Asked Questions
1. What is the difference between add() and offer() methods?
Both insert elements, but add() throws an exception if insertion fails, while offer() returns false.
2. When should I use remove() versus poll()?
Use remove() when you want an exception for empty queues, and poll() when you prefer null return values.
3. What are the main Queue implementations?
LinkedList (general-purpose), ArrayDeque (resizable array), and PriorityQueue (priority-based ordering).
Queue
- .add()
- Inserts an element at the back of the queue.
- .element()
- Returns the head of the queue without removing it.
- .isEmpty()
- Returns true if the queue contains no elements, false otherwise.
- .peek()
- Returns the head element of the queue without removing it, or null if the queue is empty.
- clear()
- Removes all elements from a Java Queue making it empty.
- contains()
- Checks whether a specific element is present in a Java Queue and returns a boolean value indicating the result
- offer()
- Inserts the specified element into the queue if possible without violating capacity limits.
- poll()
- Retrieves and removes the head of the queue, or returns null if the queue is empty.
- remove()
- Removes and returns the head (first element) of the queue, throwing an exception if the queue is empty.
- size()
- Returns the number of elements stored in a queue.
Learn Java on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.
- Beginner Friendly.17 hours