In Java, Queue
and Deque
are interfaces in the java.util
package that provide implementations for FIFO (First-In-First-Out) and double-ended queue (Deque) data structures respectively. These interfaces are part of the Java Collections Framework and offer a variety of methods for adding, removing, and inspecting elements in a queue or deque. In this tutorial, we’ll explore the Queue
and Deque
interfaces, their common implementations, and provide examples of how to use them.
1. Queue Interface
The Queue
interface represents a collection designed for holding elements before processing. It follows the FIFO (First-In-First-Out) principle, where elements are added at the end and removed from the beginning. Some common implementations of the Queue
interface include LinkedList
, PriorityQueue
, and ArrayDeque
.
Common Methods
Here are some common methods provided by the Queue
interface:
add(element)
: Adds the specified element to the end of the queue.offer(element)
: Inserts the specified element into the queue if possible.remove()
: Removes and returns the element at the front of the queue.poll()
: Retrieves and removes the element at the front of the queue, or returnsnull
if the queue is empty.element()
: Retrieves, but does not remove, the element at the front of the queue.peek()
: Retrieves, but does not remove, the element at the front of the queue, or returnsnull
if the queue is empty.
Implementation: LinkedList
import java.util.LinkedList;
import java.util.Queue;
public class QueueExample {
public static void main(String[] args) {
Queue<String> queue = new LinkedList<>();
// Adding elements to the queue
queue.add("Element 1");
queue.add("Element 2");
queue.add("Element 3");
System.out.println("Queue: " + queue);
// Removing elements from the queue
String removedElement = queue.remove();
System.out.println("Removed Element: " + removedElement);
System.out.println("Updated Queue: " + queue);
// Peek at the front element
String peekedElement = queue.peek();
System.out.println("Peeked Element: " + peekedElement);
System.out.println("Final Queue: " + queue);
}
}
2. Deque Interface
The Deque
(Double-Ended Queue) interface represents a linear collection that supports element insertion and removal at both ends. It extends the Queue
interface and provides additional methods for inserting and removing elements from both ends. Some common implementations of Deque
include ArrayDeque
and LinkedList
.
Common Methods
Here are some common methods provided by the Deque
interface:
addFirst(element)
: Adds the specified element at the beginning of the deque.addLast(element)
: Adds the specified element at the end of the deque.offerFirst(element)
: Inserts the specified element at the beginning of the deque if possible.offerLast(element)
: Inserts the specified element at the end of the deque if possible.removeFirst()
: Removes and returns the first element of the deque.removeLast()
: Removes and returns the last element of the deque.pollFirst()
: Retrieves and removes the first element of the deque, or returnsnull
if the deque is empty.pollLast()
: Retrieves and removes the last element of the deque, or returnsnull
if the deque is empty.getFirst()
: Retrieves, but does not remove, the first element of the deque.getLast()
: Retrieves, but does not remove, the last element of the deque.peekFirst()
: Retrieves, but does not remove, the first element of the deque, or returnsnull
if the deque is empty.peekLast()
: Retrieves, but does not remove, the last element of the deque, or returnsnull
if the deque is empty.
Implementation: ArrayDeque
import java.util.ArrayDeque;
import java.util.Deque;
public class DequeExample {
public static void main(String[] args) {
Deque<String> deque = new ArrayDeque<>();
// Adding elements to the deque
deque.addFirst("First");
deque.addLast("Last");
deque.add("Middle");
System.out.println("Deque: " + deque);
// Removing elements from the deque
String removedFirst = deque.removeFirst();
System.out.println("Removed First: " + removedFirst);
String removedLast = deque.removeLast();
System.out.println("Removed Last: " + removedLast);
System.out.println("Updated Deque: " + deque);
// Peek at the first and last elements
String peekFirst = deque.peekFirst();
String peekLast = deque.peekLast();
System.out.println("Peek First: " + peekFirst);
System.out.println("Peek Last: " + peekLast);
System.out.println("Final Deque: " + deque);
}
}
3. Examples
Queue Example
import java.util.LinkedList;
import java.util.Queue;
public class QueueExample {
public static void main(String[] args) {
Queue<String> queue = new LinkedList<>();
// Adding elements to the queue
queue.add("Element 1");
queue.add("Element 2");
queue.add("Element 3");
System.out.println("Queue: " + queue);
// Removing elements from the queue
String removedElement = queue.remove();
System.out.println("Removed Element: " + removedElement);
System.out.println("Updated Queue: " + queue);
// Peek at the front element
String peekedElement = queue.peek();
System.out.println("Peeked Element: " + peekedElement);
System.out.println("Final Queue: " + queue);
}
}
Deque Example
import java.util.ArrayDeque;
import java.util.Deque;
public class DequeExample {
public static void main(String[] args) {
Deque<String> deque = new ArrayDeque<>();
// Adding elements to the deque
deque.addFirst("First");
deque.addLast("Last");
deque.add("Middle");
System.out
.println("Deque: " + deque);
// Removing elements from the deque
String removedFirst = deque.removeFirst();
System.out.println("Removed First: " + removedFirst);
String removedLast = deque.removeLast();
System.out.println("Removed Last: " + removedLast);
System.out.println("Updated Deque: " + deque);
// Peek at the first and last elements
String peekFirst = deque.peekFirst();
String peekLast = deque.peekLast();
System.out.println("Peek First: " + peekFirst);
System.out.println("Peek Last: " + peekLast);
System.out.println("Final Deque: " + deque);
}
}
4. Conclusion
In this tutorial, we’ve covered the Queue
and Deque
interfaces in Java Collections. Queue
represents a collection designed for holding elements before processing in a FIFO manner, while Deque
represents a double-ended queue that allows insertion and removal at both ends. We’ve seen how to use common methods provided by these interfaces and implemented examples using LinkedList
for Queue
and ArrayDeque
for Deque
. Understanding these interfaces and their implementations can be beneficial when dealing with various data processing scenarios in Java.