Java Queue Interface
In Java, the Queue
interface is part of the java.util
package and represents a collection designed for holding elements prior to processing. It is an essential part of the Java Collections Framework and defines a collection that operates on the First-In-First-Out (FIFO) principle. This means that the first element added to the queue will be the first one to be removed, similar to how people queue up in line.
Queues are commonly used in various applications such as task scheduling, data buffering, and managing resources. In this blog post, we will explore the Queue
interface, its common implementations, methods, and provide practical examples to help you understand how to work with queues in Java.
A queue is a linear data structure that allows elements to be inserted at the back and removed from the front, adhering to the FIFO (First-In-First-Out) principle. The Queue
interface in Java extends the Collection
interface and provides a contract for various queue operations.
The Queue
interface supports the following fundamental operations:
Unlike a List
, a queue does not allow random access to its elements. Instead, elements are processed in the order they were added.
Here are some of the essential methods of the Queue
interface:
true
if successful.null
if the queue is empty.null
if the queue is empty.NoSuchElementException
if the queue is empty.Several classes in the Java Collections Framework implement the Queue
interface, each suited for different use cases:
LinkedList
for most use cases and does not allow null
elements.java.util.concurrent
package, a BlockingQueue
can be used in multi-threaded applications to block the thread until an element becomes available or space becomes available in the queue.Let’s look at practical examples using common queue implementations.
The LinkedList
class implements the Queue
interface and provides the FIFO behavior that is typical of a queue.
import java.util.LinkedList;
import java.util.Queue;
public class Main {
public static void main(String[] args) {
Queue<String> queue = new LinkedList<>();
// Adding elements to the queue
queue.offer("Apple");
queue.offer("Banana");
queue.offer("Cherry");
// Displaying the queue
System.out.println("Queue: " + queue);
// Removing and printing the front element
String removedElement = queue.poll();
System.out.println("Removed Element: " + removedElement);
// Displaying the queue after removal
System.out.println("Queue after poll: " + queue);
// Peeking at the front element
String front = queue.peek();
System.out.println("Front Element: " + front);
}
}
Output:
Queue: [Apple, Banana, Cherry]
Removed Element: Apple
Queue after poll: [Banana, Cherry]
Front Element: Banana
A PriorityQueue
allows elements to be processed according to their priority rather than in FIFO order. The elements are ordered using their natural ordering or a custom comparator.
import java.util.PriorityQueue;
import java.util.Queue;
public class Main {
public static void main(String[] args) {
Queue<Integer> priorityQueue = new PriorityQueue<>();
// Adding elements with different priorities
priorityQueue.offer(20);
priorityQueue.offer(10);
priorityQueue.offer(30);
// Displaying the queue
System.out.println("Priority Queue: " + priorityQueue);
// Removing and printing the front element (highest priority)
Integer removedElement = priorityQueue.poll();
System.out.println("Removed Element: " + removedElement);
// Displaying the queue after removal
System.out.println("Priority Queue after poll: " + priorityQueue);
}
}
Output:
Priority Queue: [10, 20, 30]
Removed Element: 10
Priority Queue after poll: [20, 30]
An ArrayDeque
is a highly efficient queue implementation. Unlike LinkedList
, ArrayDeque
does not allow null
elements and generally performs better for most use cases.
import java.util.ArrayDeque;
import java.util.Queue;
public class Main {
public static void main(String[] args) {
Queue<String> queue = new ArrayDeque<>();
// Adding elements to the queue
queue.offer("Red");
queue.offer("Green");
queue.offer("Blue");
// Displaying the queue
System.out.println("Queue: " + queue);
// Removing and printing the front element
String removedElement = queue.poll();
System.out.println("Removed Element: " + removedElement);
// Displaying the queue after removal
System.out.println("Queue after poll: " + queue);
}
}
Output:
Queue: [Red, Green, Blue]
Removed Element: Red
Queue after poll: [Green, Blue]
Queues are used in various scenarios, such as:
List
allows random access to elements and does not enforce an ordering of element removal like a queue.Deque
(double-ended queue) allows adding/removing elements from both ends, while a queue only supports removal from one end.