Java .isEmpty()
Anonymous contributor
Published Aug 7, 2025
Contribute to Docs
The .isEmpty() method is an inbuilt method of the Queue interface in Java that returns true if the queue contains no elements, and false otherwise. It is inherited from the Collection interface and provides a convenient way to check if a queue is empty before performing operations that require elements. The method has O(1) time complexity in most implementations, making it an efficient way to validate queue state.
Syntax
queueName.isEmpty()
Parameters:
- The
.isEmpty()method does not accept any parameters.
Return value:
The .isEmpty() method returns a boolean value:
trueif the queue contains no elementsfalseif the queue contains one or more elements
Example 1: Basic Usage of .isEmpty()
In this example, .isEmpty() is used to check if a queue is empty before and after adding elements:
import java.util.LinkedList;import java.util.Queue;public class Main {public static void main(String[] args) {Queue<String> queue = new LinkedList<String>();// Check if queue is empty initiallySystem.out.println("Is queue empty? " + queue.isEmpty());System.out.println("Queue contents: " + queue);// Add elements to the queuequeue.offer("First");queue.offer("Second");queue.offer("Third");// Check if queue is empty after adding elementsSystem.out.println("Is queue empty after adding elements? " + queue.isEmpty());System.out.println("Queue contents: " + queue);System.out.println("Queue size: " + queue.size());// Remove all elementsqueue.poll();queue.poll();queue.poll();// Check if queue is empty after removing all elementsSystem.out.println("Is queue empty after removing all elements? " + queue.isEmpty());System.out.println("Queue contents: " + queue);}}
The output of this code is:
Is queue empty? trueQueue contents: []Is queue empty after adding elements? falseQueue contents: [First, Second, Third]Queue size: 3Is queue empty after removing all elements? trueQueue contents: []
Example 2: Using .isEmpty() in a Loop
This example demonstrates how .isEmpty() can be used to safely process all elements in a queue:
import java.util.LinkedList;import java.util.Queue;public class Main {public static void main(String[] args) {Queue<Integer> numbers = new LinkedList<Integer>();// Add some numbers to the queuenumbers.offer(10);numbers.offer(20);numbers.offer(30);numbers.offer(40);numbers.offer(50);System.out.println("Initial queue: " + numbers);System.out.println("Processing all elements in the queue:");int elementCount = 0;// Process all elements until queue is emptywhile (!numbers.isEmpty()) {Integer number = numbers.poll();elementCount++;System.out.println("Processing element #" + elementCount + ": " + number);System.out.println("Remaining elements: " + numbers.size());}System.out.println("Queue is now empty: " + numbers.isEmpty());System.out.println("Total elements processed: " + elementCount);}}
The output of this code is:
Initial queue: [10, 20, 30, 40, 50]Processing all elements in the queue:Processing element #1: 10Remaining elements: 4Processing element #2: 20Remaining elements: 3Processing element #3: 30Remaining elements: 2Processing element #4: 40Remaining elements: 1Processing element #5: 50Remaining elements: 0Queue is now empty: trueTotal elements processed: 5
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Java on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.
- Beginner Friendly.17 hours