Home Java ArrayDeque Methods isEmpty() Java ArrayDeque isEmpty() Method 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.
Deque<String> deque = new ArrayDeque<>();
boolean empty = deque.isEmpty(); // true
deque.add("x");
empty = deque.isEmpty(); // false Call What 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.util.ArrayDeque<E>
public boolean isEmpty()
// Declared in Collection<E> Parameters Parameter Description None isEmpty() takes no parameters.
Return Value Return Value Description booleantrue if the deque contains no elements; otherwise false.
Important
isEmpty() is a constant-time check and never changes the deque. Use it before pop(), remove(), element(), or getFirst()/getLast() to avoid NoSuchElementException.
Compare with Similar Methods Ways to test for an empty deque:
Method How 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 1. true and false 2. Drain a stack 3. Balanced brackets Example 1 — isEmpty Before and After Check a new deque, then after adding and removing one element.
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 Brand new. A new deque has no elements, so the first check prints true.One element. After add("x"), the deque is not empty.Back to empty. poll() removes the only element.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.
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());
}
} pop 9
pop 4
pop 1
empty now: true How It Works Push squares. 1, 4, and 9 are pushed, so 9 ends up on top.Loop condition. !stack.isEmpty() is checked before every pop, so pop() never runs on an empty stack.Reverse order. The values come off as 9, 4, 1.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.
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()));
}
}
} (()) balanced=true
(() balanced=false
()) balanced=false How It Works (()). Two pushes and two pops; every ) finds an open bracket and the stack ends empty, so it is balanced.((). One ( is never closed, so open.isEmpty() is false at the end.()). The second ) arrives when the stack is already empty, so ok becomes false right away.Two checks. isEmpty() is used both during the scan (nothing to close) and after it (nothing left open).Frequently Asked Questions What does ArrayDeque isEmpty() return? true if the deque has no elements, false if it has at least one.
Is isEmpty() the same as size() == 0? Yes, it gives the same answer and is easier to read.
Does isEmpty() work on a null deque variable? No. Calling any method on a null reference throws NullPointerException.
Why use isEmpty() before pop()? pop() throws NoSuchElementException on an empty deque. Checking isEmpty() first avoids the exception.
Can peek() == null replace isEmpty()? For ArrayDeque, yes, because it never stores null. isEmpty() is still clearer.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion