Introduction
The Deque
interface in Java, part of the java.util
package, represents a double-ended queue that allows elements to be added or removed from both ends.
Table of Contents
- What is the
Deque
Interface? - Common Implementations
- Common Methods
- Examples of Using the
Deque
Interface - Conclusion
1. What is the Deque Interface?
The Deque
(Double-Ended Queue) interface extends the Queue
interface and provides methods for manipulating elements at both the front and back of the queue.
2. Common Implementations
- ArrayDeque: A resizable array implementation that is efficient for most operations. It is not thread-safe.
- LinkedList: Provides a linked list implementation of the
Deque
interface, allowing efficient insertion and removal of elements from both ends.
3. Common Methods
addFirst(E e)
: Inserts the specified element at the front.addLast(E e)
: Inserts the specified element at the end.offerFirst(E e)
: Inserts the specified element at the front, returnstrue
if successful.offerLast(E e)
: Inserts the specified element at the end, returnstrue
if successful.removeFirst()
: Removes and returns the first element.removeLast()
: Removes and returns the last element.pollFirst()
: Retrieves and removes the first element, or returnsnull
if empty.pollLast()
: Retrieves and removes the last element, or returnsnull
if empty.getFirst()
: Retrieves, but does not remove, the first element.getLast()
: Retrieves, but does not remove, the last element.peekFirst()
: Retrieves, but does not remove, the first element, or returnsnull
.peekLast()
: Retrieves, but does not remove, the last element, or returnsnull
.
4. Examples of Using the Deque Interface
Example 1: Using addFirst
and addLast
This example demonstrates adding elements to both ends of a Deque
.
import java.util.ArrayDeque;
import java.util.Deque;
public class AddExample {
public static void main(String[] args) {
Deque<String> deque = new ArrayDeque<>();
deque.addFirst("Front");
deque.addLast("Back");
System.out.println("Deque after additions: " + deque);
}
}
Output:
Deque after additions: [Front, Back]
Example 2: Using offerFirst
and offerLast
This example shows how to use offerFirst
and offerLast
to add elements.
import java.util.ArrayDeque;
import java.util.Deque;
public class OfferExample {
public static void main(String[] args) {
Deque<Integer> deque = new ArrayDeque<>();
deque.offerFirst(1);
deque.offerLast(2);
System.out.println("Deque after offers: " + deque);
}
}
Output:
Deque after offers: [1, 2]
Example 3: Using removeFirst
and removeLast
This example demonstrates removing elements from both ends of a Deque
.
import java.util.ArrayDeque;
import java.util.Deque;
public class RemoveExample {
public static void main(String[] args) {
Deque<String> deque = new ArrayDeque<>();
deque.add("A");
deque.add("B");
System.out.println("Removed from front: " + deque.removeFirst());
System.out.println("Removed from back: " + deque.removeLast());
}
}
Output:
Removed from front: A
Removed from back: B
Example 4: Using pollFirst
and pollLast
This example shows how to use pollFirst
and pollLast
to retrieve and remove elements.
import java.util.ArrayDeque;
import java.util.Deque;
public class PollExample {
public static void main(String[] args) {
Deque<Integer> deque = new ArrayDeque<>();
deque.add(10);
deque.add(20);
System.out.println("Polled from front: " + deque.pollFirst());
System.out.println("Polled from back: " + deque.pollLast());
}
}
Output:
Polled from front: 10
Polled from back: 20
Example 5: Using getFirst
and getLast
This example demonstrates retrieving elements from both ends without removal.
import java.util.ArrayDeque;
import java.util.Deque;
public class GetExample {
public static void main(String[] args) {
Deque<String> deque = new ArrayDeque<>();
deque.add("First");
deque.add("Last");
System.out.println("First element: " + deque.getFirst());
System.out.println("Last element: " + deque.getLast());
}
}
Output:
First element: First
Last element: Last
Example 6: Using peekFirst
and peekLast
This example shows how to peek at elements at both ends of a Deque
.
import java.util.ArrayDeque;
import java.util.Deque;
public class PeekExample {
public static void main(String[] args) {
Deque<String> deque = new ArrayDeque<>();
deque.add("Start");
deque.add("End");
System.out.println("Peek first: " + deque.peekFirst());
System.out.println("Peek last: " + deque.peekLast());
}
}
Output:
Peek first: Start
Peek last: End
5. Conclusion
The Deque
interface in Java provides versatile methods for working with elements at both ends of a queue. It is useful for scenarios requiring double-ended queue operations, enhancing flexibility in data manipulation. Common implementations like ArrayDeque
and LinkedList
provide efficient options for different use cases.
Comments
Post a Comment
Leave Comment