Deque
A double-ended queue: elements can be added and taken at both ends.
Available since: Java 1.6
Syntax
public interface Deque<E> extends Queue<E>, SequencedCollection<E>Examples
Operations on both ends:
void main() {
Deque<Integer> d = new ArrayDeque<>();
d.offerLast(2);
d.offerFirst(1);
d.offerLast(3);
IO.println(d);
IO.println(d.pollFirst() + " " + d.pollLast());
IO.println(d);
}Output:
[1, 2, 3]
1 3
[2]Notes
The same Deque works as a queue (offer() + poll()) and as a stack (push() + pop()). Each operation has First and Last versions. Implementations: ArrayDeque (the usual choice) and LinkedList.
See also
- ArrayDeque — A fast array-based double-ended queue — a replacement for `Stack` and `LinkedList`.
- Deque.push() — Pushes an element onto the top of a stack (the head of a deque).
- Deque.pop() — Removes and returns the element at the top of a stack.
- Queue — A queue: elements are added at the tail and taken from the head.
In the tutorial
- Queues and deques — Queues in Java: Queue and ArrayDeque, a stack built on Deque, the offer, poll and peek methods and the PriorityQueue.