The isEmpty()
method is a simple yet essential operation available in the Java ArrayList
class. It allows you to check whether an ArrayList contains any elements or not. This method is widely used for validating collection state before performing actions that require a non-empty list.
What Is the isEmpty() Method in ArrayList?
- The isEmpty() method returns
true
if theArrayList
contains no elements. - It returns
false
if there is at least one element present in the list. - This method is mainly used to prevent errors by ensuring a list is not empty before processing or accessing its elements.
Syntax
public boolean isEmpty()
Parameters
- The
isEmpty()
method does not take any parameters.
Return Value
Returns | Description |
---|---|
true | If the ArrayList has zero elements |
false | If the ArrayList contains one or more elements |
Exceptions
- This method does not throw any exceptions under normal circumstances.
- It’s supported by all standard
ArrayList
implementations.
How Does isEmpty() Work Internally?
- Internally,
isEmpty()
checks whether the list’s size is zero (size() == 0
). - This check is performed in constant time O(1), making it very efficient.
- No elements are accessed or modified.
Examples of the isEmpty() Method
1. Check if an ArrayList is Empty
import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<String> names = new ArrayList<>(); System.out.println("Is the list empty? " + names.isEmpty()); // true names.add("Alice"); System.out.println("Is the list empty? " + names.isEmpty()); // false } }
Output:
Is the list empty? true
Is the list empty? false
2. Using isEmpty() Before Accessing Elements
import java.util.ArrayList; public class Demo { public static void main(String[] args) { ArrayList<Integer> numbers = new ArrayList<>(); if (!numbers.isEmpty()) { System.out.println("First item: " + numbers.get(0)); } else { System.out.println("The list is empty. Cannot access elements."); } } }
Output:
The list is empty. Cannot access elements.
3. Clearing a List and Using isEmpty()
import java.util.ArrayList; public class Example { public static void main(String[] args) { ArrayList<String> items = new ArrayList<>(); items.add("Book"); items.add("Pen"); System.out.println("Before clear: " + items.isEmpty()); // false items.clear(); System.out.println("After clear: " + items.isEmpty()); // true } }
Output:
Before clear: false
After clear: true
Important Notes
- The
isEmpty()
method is preferred oversize() == 0
for readability and intent. - It is a safe way to check for emptiness before performing actions like remove, get, or iteration.
- Works for all data types stored in
ArrayList
, whether primitives (as wrappers) or custom objects. - If you reuse lists by clearing them with
clear()
, usingisEmpty()
is effective for validation.
Summary
Aspect | Details |
---|---|
Method Purpose | Check if an ArrayList contains no elements |
Syntax | list.isEmpty() |
Return Value | true if empty, false if not empty |
Parameters | None |
Exceptions | None (in standard usage) |
Typical Use Cases | Validation, control flow, error prevention |
Typical Use Cases
- Validating user input stored in a list before processing.
- Checking a list before iteration to avoid exceptions.
- Conditionally displaying information or performing actions only if the list is not empty.
- Controlling program flow where further actions depend on data availability.