Java ArrayDeque size() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
ArrayDeque

Definition

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
CallWhat it does
deque.size()Number of elements, as an int.
deque.size() == 0Same 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

ParameterDescription
Nonesize() takes no parameters.

Return Value

Return ValueDescription
intThe number of elements in the deque. 0 for an empty deque.

Compare with Similar Methods

Methods that tell you about the deque's contents:

MethodHow it differs
isEmpty()Only answers "zero or not"; reads better than size() == 0.
contains(o)Asks about one specific value.
toArray().lengthSame number, but copies every element first: wasteful.
clear()Brings the size back to 0.

Java Examples

Example 1 — Watch the Size Change

Add three elements in three different ways, then remove one.

Java
import java.util.ArrayDeque;
import java.util.Deque;

public class ArrayDequeSizeDemo {
    public static void main(String[] args) {
        Deque<String> d = new ArrayDeque<>();
        System.out.println(d.size());

        d.add("a");
        d.addFirst("b");
        d.push("c");
        System.out.println(d.size());

        d.pop();
        System.out.println(d.size() + " " + d);
    }
}

How It Works

  1. New deque. It starts with no elements, so size() is 0.
  2. Any insert counts. add, addFirst, and push each add one element, giving 3.
  3. Removal. pop() removes the head, c, so the size drops to 2.
  4. What is left. b (added with addFirst) and a remain: [b, a].

Example 2 — The Constructor Number Is Not the Size

Reserve room for 100 elements, check the size, then add 150 elements.

Java
import java.util.ArrayDeque;
import java.util.Deque;

public class ArrayDequeSizeHint {
    public static void main(String[] args) {
        Deque<Integer> d = new ArrayDeque<>(100);
        System.out.println("size with room for 100: " + d.size());

        for (int i = 0; i < 150; i++) {
            d.add(i);
        }
        System.out.println("size now: " + d.size());
        System.out.println("first " + d.peekFirst() + ", last " + d.peekLast());
    }
}

How It Works

  1. Room, not elements. The constructor argument only sizes the internal array, so the deque is still empty.
  2. Past the hint. The loop adds 150 numbers, more than the 100 reserved.
  3. Automatic growth. The deque enlarges its array when needed; no add fails, and size() reports 150.
  4. Ends check. The head is 0 and the tail is 149, the first and last values added.

Example 3 — A Classic size() Loop Bug

Polling inside a loop that re-checks size() stops halfway. Saving the size first fixes it.

Java
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;

public class ArrayDequeSizeLoopBug {
    public static void main(String[] args) {
        Deque<Integer> d = new ArrayDeque<>(Arrays.asList(1, 2, 3, 4, 5, 6));

        int count = 0;
        for (int i = 0; i < d.size(); i++) {
            d.poll();
            count++;
        }
        System.out.println("polled " + count + ", left " + d);

        int n = d.size();
        for (int i = 0; i < n; i++) {
            d.poll();
        }
        System.out.println("after fixed loop: " + d);
    }
}

How It Works

  1. A moving target. The first loop compares i with d.size(), which shrinks by one on every pass.
  2. Where it stops. After three polls, i is 3 and the size is 3, so 3 < 3 is false and the loop ends early.
  3. Half done. Only 1, 2, and 3 were polled; [4, 5, 6] remain.
  4. The fix. Storing the size in n before the second loop makes it run exactly three times, emptying the deque.

Frequently Asked Questions

The number of elements currently in the deque, as an int.
No. The number is only a starting room hint. The new deque has size 0.
No. Its internal array grows automatically and there is no capacity limit or capacity getter.
Yes, it runs in constant time.
Both work. isEmpty() states the intent more clearly.

Next: spliterator()

Get a Spliterator for streams and parallel splitting.

spliterator() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

13 people found this page helpful