For working professionals
For fresh graduates
More
6. JDK in Java
7. C++ Vs Java
16. Java If-else
18. Loops in Java
20. For Loop in Java
46. Packages in Java
53. Java Collection
56. Generics In Java
57. Java Interfaces
60. Streams in Java
63. Thread in Java
67. Deadlock in Java
74. Applet in Java
75. Java Swing
76. Java Frameworks
78. JUnit Testing
81. Jar file in Java
82. Java Clean Code
86. Java 8 features
87. String in Java
93. HashMap in Java
98. Enum in Java
101. Hashcode in Java
105. Linked List in Java
109. Array Length in Java
111. Split in java
112. Map In Java
115. HashSet in Java
118. DateFormat in Java
121. Java List Size
122. Java APIs
128. Identifiers in Java
130. Set in Java
132. Try Catch in Java
133. Bubble Sort in Java
135. Queue in Java
142. Jagged Array in Java
144. Java String Format
145. Replace in Java
146. charAt() in Java
147. CompareTo in Java
151. parseInt in Java
153. Abstraction in Java
154. String Input in Java
156. instanceof in Java
157. Math Floor in Java
158. Selection Sort Java
159. int to char in Java
164. Deque in Java
172. Trim in Java
173. RxJava
174. Recursion in Java
175. HashSet Java
177. Square Root in Java
190. Javafx
In Java programming, a Queue is a vital data structure that stores elements in a sequential manner, following the First-In, First-Out (FIFO) principle. It ensures that the element added first is the one removed first, making it ideal for managing real-world processes like task scheduling, printer queues, and order processing systems. Java programming provides the Queue interface under the java.util package, along with multiple implementations. In this guide, we’ll explore how the Queue in Java works, its basic operations, and its implementation in Java.
Build a strong career in Java programming — check out upGrad’s Software Engineering program today!
A Queue is a linear data structure that follows the First-In, First-Out (FIFO) principle. This means the element that is inserted first is the one that gets removed first. Think of a queue like a line at a ticket counter — the person who arrives first is served first.
In Java, the Queue is an interface provided in the java.util package. Classes like LinkedList, PriorityQueue, and ArrayDeque implement this interface to provide various types of queue behaviors.
Shape a successful career in AI and data science with these top academic programs:
Basic Operations of Queue
Several key operations are performed to manage data inside a queue. Understanding these basic operations of a queue is essential before diving into implementation.
Enqueue adds an element to the rear (end) of the queue. In Java, this is done using the add() or offer() methods.
Dequeue removes an element from the front of the queue. In Java, you can perform this using the remove() or poll() methods.
IsEmpty checks whether the queue has any elements or not. In Java, calling isEmpty() returns true if the queue is empty.
IsFull is more relevant for fixed-size queues (like arrays). It checks if the queue cannot accept more elements. In dynamic structures like LinkedList, this isn’t a concern.
Peek retrieves the element at the front of the queue without removing it. You can use the peek() or element() method in Java.
Must read: Difference Between Linear and Non-Linear Data Structures
Here’s how the working of a queue looks step-by-step:
This ensures that the first element inserted is always the first to come out, preserving FIFO behavior.
Also check: Stack and Queue in Java
Let's see how you can perform a basic Queue implementation in Java using the LinkedList class.
Suppose you are managing a print job queue, where the first document sent to the printer must be printed first.
import java.util.LinkedList;
import java.util.Queue;
public class PrintJobQueue {
public static void main(String[] args) {
Queue<String> printQueue = new LinkedList<>();
// Enqueue elements
printQueue.add("Document1");
printQueue.add("Document2");
printQueue.add("Document3");
System.out.println("Print Queue: " + printQueue);
// Peek at the front document
System.out.println("First document to print: " + printQueue.peek());
// Dequeue elements
printQueue.remove();
System.out.println("Queue after printing one document: " + printQueue);
// Check if the queue is empty
System.out.println("Is print queue empty? " + printQueue.isEmpty());
}
}
Check out: Difference between LinkedList and Array in Java
import java.util.LinkedList;
import java.util.Queue;
public class Main {
public static void main(String[] args) {
Queue<String> queue = new LinkedList<>();
queue.add("Apple");
System.out.println(queue);
}
}
Output:
[Apple]
Explanation: The add() method inserts an element at the rear of the queue. It throws an exception if the queue is capacity-restricted and full (not an issue with LinkedList).
Must explore: Types of Linked List in Java
import java.util.LinkedList;
import java.util.Queue;
public class Main {
public static void main(String[] args) {
Queue<String> queue = new LinkedList<>();
queue.offer("Banana");
System.out.println(queue);
}
}
Output:
[Banana]
Explanation:
The offer() method also adds an element to the rear but returns false if insertion fails instead of throwing an exception, making it safer for limited-capacity queues.
Also read: Doubly Linked List Data Structures: A Complete Guide
import java.util.LinkedList;
import java.util.Queue;
public class Main {
public static void main(String[] args) {
Queue<String> queue = new LinkedList<>();
queue.add("Cat");
System.out.println(queue.remove());
}
}
Output
Cat
Explanation:
The remove() method deletes and returns the front element. If the queue is empty, it throws a NoSuchElementException.
Must explore: Recursion in Data Structures: Types, Algorithms, and Applications
import java.util.LinkedList;
import java.util.Queue;
public class Main {
public static void main(String[] args) {
Queue<String> queue = new LinkedList<>();
queue.add("Dog");
System.out.println(queue.poll());
}
}
Output:
Dog
Explanation:
The poll() method also removes the front element but returns null instead of throwing an exception if the queue is empty.
Also explore: Stack and Heap Memory in Java: Key Differences Explained
import java.util.LinkedList;
import java.util.Queue;
public class Main {
public static void main(String[] args) {
Queue<String> queue = new LinkedList<>();
queue.add("Elephant");
System.out.println(queue.peek());
}
}
Output:
Elephant
Explanation:
The peek() method retrieves but does not remove the head of the queue. If the queue is empty, it returns null.
6. element() – View front element strictly
import java.util.LinkedList;
import java.util.Queue;
public class Main {
public static void main(String[] args) {
Queue<String> queue = new LinkedList<>();
queue.add("Fish");
System.out.println(queue.element());
}
}
Output:
Fish
Explanation:
The element() method also retrieves the front without removal, but throws a NoSuchElementException if the queue is empty instead of returning null.
Queue in Java is a fundamental concept every developer should master. It ensures first-come, first-served data management, which is critical in many real-world scenarios like scheduling, task management, and system resource handling. By understanding basic queue operations, the working of a queue, and Java queue implementation, you can design more efficient and scalable applications.
Queue in Java follows the First-In, First-Out (FIFO) principle. Elements are inserted at the rear and removed from the front. Java provides the Queue interface and classes like LinkedList and PriorityQueue to efficiently manage the ordering and processing of elements internally.
Java supports several types of queues: Simple Queue, Circular Queue, Priority Queue, and Deque (Double-Ended Queue). Each type modifies how elements are inserted, removed, or prioritized, depending on specific requirements like order maintenance or priority-based processing.
LinkedList is often used because it allows constant time insertion and removal from both ends. It implements the Queue interface directly and does not require resizing like arrays, making it perfect for dynamic memory and real-time queue operations.
Both add() and offer() insert elements into the queue. The difference is:
Both remove() and poll() remove and return the head element.
The peek() method retrieves, but does not remove, the head of the queue. If the queue is empty, it returns null. It’s helpful when you want to check the next element to process without actually removing it.
Yes, but PriorityQueue does not maintain FIFO order. It orders elements based on their natural ordering or a provided comparator. It is used when processing order depends on priority rather than the sequence of insertion.
No, standard queues like LinkedList and PriorityQueue are not thread-safe. For thread-safe operations, you should use ConcurrentLinkedQueue, BlockingQueue, or wrap the queue with Collections.synchronizedQueue() for synchronization.
Some queue implementations like LinkedList allow null elements, but others like PriorityQueue do not allow null values. Using nulls can cause NullPointerException in queues that rely on comparison or ordering of elements.
BlockingQueue is a special type of queue that blocks the operation:
A Deque (Double-Ended Queue) allows insertion and removal of elements from both ends — front and rear. A normal Queue allows insertion at the rear and removal only from the front, following strict FIFO behavior.
Take the Free Quiz on Java
Answer quick questions and assess your Java knowledge
Author|900 articles published
Previous
Next
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918068792934
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.