In Java, size() returns the number of elements currently stored in an ArrayDeque, as an int. It goes up by one with every successful add and down by one with every removal.
It is not the same as the number you can pass to the constructor. new ArrayDeque<>(100) only reserves room; its size() is still 0. An ArrayDeque has no capacity method, because its internal array grows by itself.
An instance method of java.util.ArrayDeque (Java 1.6+): a resizable-array deque with no capacity limit, no null elements, and no thread safety.
Syntax
Call size with no arguments.
Java
Deque<String> deque = new ArrayDeque<>();
int n = deque.size(); // 0
deque.add("a");
n = deque.size(); // 1
Call
What it does
deque.size()
Number of elements, as an int.
deque.size() == 0
Same answer as isEmpty().
deque.isEmpty()
Clearer way to check for zero elements.
new ArrayDeque<>(n)
Sets a starting room, not the size.
size() runs in constant time; the deque keeps track of where its head and tail are, so it does not count elements one by one.
Library declarations
Java
// java.util.ArrayDeque<E>
public int size()
// Declared in Collection<E> and Deque<E>
Parameters
Parameter
Description
None
size() takes no parameters.
Return Value
Return Value
Description
int
The number of elements in the deque. 0 for an empty deque.