Java ArrayDeque Methods

About This Index
This page is the Java ArrayDeque Methods index. Each row links to a full guide with syntax, parameters, return value, three runnable examples, and FAQs. Every example is a complete Java class with a main method and fixed data, so the output shown is the output you get.
Learn what an ArrayDeque is on the ArrayDeque hub. Use this page when you need a specific method such as push(), poll(), or peekFirst(). All examples compile on Java 8 and later.
An ArrayDeque has two ends. The head (first, front) is where push, pop, poll, and peek work. The tail (last, back) is where add and offer put new elements. Printing a deque shows the elements from head to tail, like [a, b, c].
How to Call an ArrayDeque Method
Every method here is an instance method, so you call it on your deque variable:
import java.util.ArrayDeque;
import java.util.Deque;
Deque<String> deque = new ArrayDeque<>();
deque.addLast("b"); // [b]
deque.addFirst("a"); // [a, b]
deque.offer("c"); // [a, b, c] (tail)
String head = deque.peek(); // "a" (looks only)
String gone = deque.poll(); // "a" (removes the head)
int count = deque.size(); // 2 Java ArrayDeque Methods Table
All method guides in this Java tutorial series (36 tutorials), grouped by purpose.
Add elements
8 methodsPut new elements at the head (front) or the tail (back). The add* and push methods return nothing or true; the offer* methods return true. All of them throw NullPointerException for null.
| Method | Description | Tutorial |
|---|---|---|
add() | Append an element at the tail and get true back. | Try it |
addAll() | Append every element of another collection at the tail. | Try it |
addFirst() | Insert an element at the head (front) of the deque. | Try it |
addLast() | Insert an element at the tail (end) of the deque. | Try it |
offer() | Queue-style insert at the tail that returns true. | Try it |
offerFirst() | Insert an element at the head and return true. | Try it |
offerLast() | Insert an element at the tail and return true. | Try it |
push() | Stack-style insert at the head of the deque. | Try it |
Remove from the ends
7 methodsTake an element off the head or the tail and get it back. The remove* and pop methods throw NoSuchElementException on an empty deque; the poll* methods return null instead.
| Method | Description | Tutorial |
|---|---|---|
remove() | Remove the head, or the first match of a value. | Try it |
removeFirst() | Remove and return the head; throws if empty. | Try it |
removeLast() | Remove and return the tail; throws if empty. | Try it |
poll() | Remove and return the head, or null if empty. | Try it |
pollFirst() | Remove and return the head, or null if empty. | Try it |
pollLast() | Remove and return the tail, or null if empty. | Try it |
pop() | Stack-style removal of the head; throws if empty. | Try it |
Look without removing
6 methodsRead the head or the tail and leave the deque unchanged. element and get* throw on an empty deque; peek* return null.
| Method | Description | Tutorial |
|---|---|---|
element() | Read the head without removing it; throws if empty. | Try it |
getFirst() | Read the first element; throws if empty. | Try it |
getLast() | Read the last element; throws if empty. | Try it |
peek() | Read the head without removing it, or get null. | Try it |
peekFirst() | Read the first element, or get null if empty. | Try it |
peekLast() | Read the last element, or get null if empty. | Try it |
Search and remove by value
6 methodsCheck whether a value is present, or remove elements by value, by another collection, or by a condition.
| Method | Description | Tutorial |
|---|---|---|
contains() | Check whether the deque holds a value (uses equals). | Try it |
removeFirstOccurrence() | Remove the first element equal to a value. | Try it |
removeLastOccurrence() | Remove the last element equal to a value. | Try it |
removeAll() | Remove every element that is in another collection. | Try it |
retainAll() | Keep only the elements that are in another collection. | Try it |
removeIf() | Remove every element that matches a condition. | Try it |
Iterate, copy, and convert
6 methodsWalk the elements head to tail or tail to head, split them for streams, copy them into an array, or clone the whole deque.
| Method | Description | Tutorial |
|---|---|---|
iterator() | Walk the elements from head to tail. | Try it |
descendingIterator() | Walk the elements from tail to head (reverse order). | Try it |
forEach() | Run an action (lambda) for every element, head to tail. | Try it |
spliterator() | Get a Spliterator for streams and parallel splitting. | Try it |
toArray() | Copy the elements into an array, head to tail. | Try it |
clone() | Make a shallow copy of the whole deque. | Try it |
Size and state
3 methodsCount the elements, check for an empty deque, or remove everything at once.
Frequently Asked Questions
Start with add()
Put your first elements into an ArrayDeque and see where they go.
12 people found this page helpful
