Java ArrayDeque isEmpty() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
ArrayDeque

Definition

In Java, isEmpty() returns true if an ArrayDeque contains no elements, and false otherwise. It is the same as checking size() == 0.

The most common use is a while (!deque.isEmpty()) loop that processes a queue or stack until nothing is left. It is also the guard that makes throwing methods like pop() and getFirst() safe to call.

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 isEmpty with no arguments.

Java
Deque<String> deque = new ArrayDeque<>();
boolean empty = deque.isEmpty();   // true
deque.add("x");
empty = deque.isEmpty();           // false
CallWhat it does
deque.isEmpty()true if there are no elements.
!deque.isEmpty()true if there is at least one element.
deque.size() == 0Same answer, slightly less readable.
deque.peek() == nullAlso means empty for an ArrayDeque, since it never holds null.

isEmpty() does not check whether the variable is null. Calling it on a null reference throws NullPointerException; create the deque before using it.

Library declarations
Java
// java.util.ArrayDeque<E>
public boolean isEmpty()

// Declared in Collection<E>

Parameters

ParameterDescription
NoneisEmpty() takes no parameters.

Return Value

Return ValueDescription
booleantrue if the deque contains no elements; otherwise false.

Compare with Similar Methods

Ways to test for an empty deque:

MethodHow it differs
size()Gives the exact count; use it when you need the number.
peek() == nullWorks too, and also gives you the head when it is not empty.
poll() loopwhile ((x = deque.poll()) != null) combines the check and the removal.
clear()Makes the deque empty, after which isEmpty() is true.

Java Examples

Example 1 — isEmpty Before and After

Check a new deque, then after adding and removing one element.

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

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

        d.add("x");
        System.out.println(d.isEmpty());

        d.poll();
        System.out.println(d.isEmpty());
    }
}

How It Works

  1. Brand new. A new deque has no elements, so the first check prints true.
  2. One element. After add("x"), the deque is not empty.
  3. Back to empty. poll() removes the only element.
  4. Same as size. Each answer matches size() == 0 at that moment.

Example 2 — Pop Until the Stack Is Empty

Push three squares, then use while (!stack.isEmpty()) to pop them all.

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

public class ArrayDequeDrainStack {
    public static void main(String[] args) {
        Deque<Integer> stack = new ArrayDeque<>();
        for (int i = 1; i <= 3; i++) {
            stack.push(i * i);
        }

        while (!stack.isEmpty()) {
            System.out.println("pop " + stack.pop());
        }
        System.out.println("empty now: " + stack.isEmpty());
    }
}

How It Works

  1. Push squares. 1, 4, and 9 are pushed, so 9 ends up on top.
  2. Loop condition. !stack.isEmpty() is checked before every pop, so pop() never runs on an empty stack.
  3. Reverse order. The values come off as 9, 4, 1.
  4. Done. After the loop, isEmpty() is true.

Example 3 — Check Balanced Parentheses

Push each (; for each ), the stack must not be empty. At the end it must be empty again.

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

public class ArrayDequeBalanced {
    public static void main(String[] args) {
        String[] inputs = {"(())", "(()", "())"};

        for (String s : inputs) {
            Deque<Character> open = new ArrayDeque<>();
            boolean ok = true;
            for (char c : s.toCharArray()) {
                if (c == '(') {
                    open.push(c);
                } else if (open.isEmpty()) {
                    ok = false;
                    break;
                } else {
                    open.pop();
                }
            }
            System.out.println(s + " balanced=" + (ok && open.isEmpty()));
        }
    }
}

How It Works

  1. (()). Two pushes and two pops; every ) finds an open bracket and the stack ends empty, so it is balanced.
  2. ((). One ( is never closed, so open.isEmpty() is false at the end.
  3. ()). The second ) arrives when the stack is already empty, so ok becomes false right away.
  4. Two checks. isEmpty() is used both during the scan (nothing to close) and after it (nothing left open).

Frequently Asked Questions

true if the deque has no elements, false if it has at least one.
Yes, it gives the same answer and is easier to read.
No. Calling any method on a null reference throws NullPointerException.
pop() throws NoSuchElementException on an empty deque. Checking isEmpty() first avoids the exception.
For ArrayDeque, yes, because it never stores null. isEmpty() is still clearer.

Next: iterator()

Walk the elements from head to tail.

iterator() 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.

10 people found this page helpful