Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Java ArrayList isEmpty(): Checking for Empty Lists

Learn how to use ArrayList.isEmpty() in Java to check whether a list contains elements, including internal behavior, null handling, and performance.

~4 min readJava
#Java#ArrayList#Collections#isEmpty
Illustration of an empty ArrayList container with the isEmpty method returning true in Java

When you need to determine whether an ArrayList contains any elements, Java provides the isEmpty() method directly on the List interface. Calling isEmpty() is the most direct way to answer the question “is this list empty?” and it reads naturally in conditionals:

java
import java.util.ArrayList; ArrayList<String> names = new ArrayList<>(); boolean empty = names.isEmpty(); // true names.add("Ada"); boolean stillEmpty = names.isEmpty(); // false

The method returns true when the list has zero elements and false when it contains at least one. isEmpty() is declared on the Collection interface, which List extends. In ArrayList, the implementation is a direct field comparison rather than a scan, and the observable behavior is identical across all standard List implementations. Code written against isEmpty() works uniformly whether the underlying object is an ArrayList, a LinkedList, or an unmodifiable list from Collections.unmodifiableList().

How isEmpty() Works Internally

The ArrayList implementation stores elements in a backing array and tracks the number of elements in a size field. The isEmpty() method is implemented as a direct field comparison:

java
public boolean isEmpty() { return size == 0; }

No iteration, no allocation, no locking. The method runs in O(1) constant time regardless of how many elements the list has ever contained. A list that previously held a million elements and was cleared still returns true from isEmpty() instantly, because the check is purely against the current size field.

This internal detail matters when you compare isEmpty() to alternatives like list.size() > 0 or a stream-based check. The stream approach allocates stream objects and is harder to read; size() > 0 reads the same size field, but isEmpty() expresses the intent of the check more directly.

isEmpty() vs size() == 0

Both names.isEmpty() and names.size() == 0 produce the same boolean result. The difference is readability and intent.

java
// Both produce the same result boolean a = names.isEmpty(); boolean b = names.size() == 0;

isEmpty() communicates the question being asked — “is this collection empty?” — without exposing the implementation detail that emptiness is measured by counting elements. size() == 0 is functionally equivalent, and some developers prefer it when they already need the size for another purpose:

java
if (names.size() > 0 && names.size() <= MAX_BATCH) { processBatch(names); }

In that case, calling both isEmpty() and size() would read the size field twice. The practical difference is negligible, but isEmpty() is the clearer choice when you only need the emptiness check.

Common Patterns for Using isEmpty()

The most frequent use of isEmpty() is as a guard clause before processing a collection. Consider a method that sends notifications to a list of recipients:

java
public void notifyUsers(ArrayList<String> userIds) { if (userIds.isEmpty()) { return; } for (String id : userIds) { sendNotification(id); } }

The early return avoids entering the loop and calling sendNotification zero times, which would be harmless but noisy. More importantly, the guard makes the no-data case explicit at the top of the method.

Another common pattern is conditional data preparation:

java
ArrayList<String> errors = validateForm(formData); if (!errors.isEmpty()) { displayErrors(errors); } else { submitForm(formData); }

Here !errors.isEmpty() reads more naturally than errors.size() > 0 and makes the branch condition obvious to anyone reading the code later. isEmpty() also appears in test assertions:

java
assertTrue(resultList.isEmpty());

JUnit's assertTrue combined with isEmpty() is a common way to verify that a method returned no results.

The Null Reference Trap

isEmpty() does not protect against a null reference. If the variable holding the ArrayList is itself null, calling isEmpty() throws a NullPointerException:

java
ArrayList<String> list = null; boolean empty = list.isEmpty(); // NullPointerException

This is a frequent source of bugs when lists are passed between methods or populated from external data. The check you need depends on what null means in your context. If null is a legitimate state meaning “no data provided,” you need an explicit null check first:

java
if (list == null || list.isEmpty()) { // treat null and empty the same way }

If null is a programming error that should fail fast, then letting the NullPointerException propagate is the correct behavior. The distinction is a design decision about your API contract, not something isEmpty() can resolve for you.

Performance Characteristics

Because isEmpty() is a single field comparison, it is one of the cheapest operations in the ArrayList API. There is no reason to avoid calling it in hot paths or inside loops.

A common performance misconception is that isEmpty() scans the backing array. It does not. The size field is maintained incrementally as elements are added and removed, so the emptiness check never depends on the array's capacity or its historical size.

java
ArrayList<String> large = new ArrayList<>(1_000_000); // populate and then clear large.clear(); boolean empty = large.isEmpty(); // O(1), not O(n)

The emptiness check depends only on the current size, not on the contents of the backing array. In clear(), the array slots are set to null to allow garbage collection, but that work belongs to clear(), not to isEmpty().

When to Use Other Checks

There are situations where isEmpty() is not the right tool. If you need to know not just whether the list is empty but also whether the list variable is null, you need the combined check shown earlier. If you need to distinguish between “empty” and “contains only blank strings,” isEmpty() is insufficient:

java
ArrayList<String> values = new ArrayList<>(); values.add(" "); boolean empty = values.isEmpty(); // false, the list has one element

The list is not empty even though the string it contains is whitespace. Testing whether every element is blank requires a stream operation or a loop:

java
boolean allBlank = values.stream().allMatch(s -> s.trim().isEmpty());

Similarly, if you are working with a Collection that might be a lazy view rather than a materialized list, isEmpty() on that view may trigger computation. For the standard ArrayList, however, the check is always immediate and cheap. When you need to distinguish between an empty list and a list containing only blank or default values, consider whether isEmpty() alone answers the question your business logic is actually asking.

Try matching browser tools

All tools

Small utilities that fit Java work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Java guides

More in Java

All Java guides

The guides filed next to this one in Java, in library order.