|
| 1 | +--- |
| 2 | +Title: '.isEmpty()' |
| 3 | +Description: 'Returns true if the queue contains no elements, false otherwise.' |
| 4 | +Subjects: |
| 5 | + - 'Code Foundations' |
| 6 | + - 'Computer Science' |
| 7 | +Tags: |
| 8 | + - 'Collections' |
| 9 | + - 'Data Structures' |
| 10 | + - 'Methods' |
| 11 | + - 'Queues' |
| 12 | +CatalogContent: |
| 13 | + - 'learn-java' |
| 14 | + - 'paths/computer-science' |
| 15 | +--- |
| 16 | + |
| 17 | +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`](https://www.codecademy.com/resources/docs/java/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. |
| 18 | + |
| 19 | +## Syntax |
| 20 | + |
| 21 | +```pseudo |
| 22 | +queueName.isEmpty() |
| 23 | +``` |
| 24 | + |
| 25 | +**Parameters:** |
| 26 | + |
| 27 | +- The `.isEmpty()` method does not accept any parameters. |
| 28 | + |
| 29 | +**Return value:** |
| 30 | + |
| 31 | +The `.isEmpty()` method returns a `boolean` value: |
| 32 | + |
| 33 | +- `true` if the queue contains no elements |
| 34 | +- `false` if the queue contains one or more elements |
| 35 | + |
| 36 | +## Example 1: Basic Usage of `.isEmpty()` |
| 37 | + |
| 38 | +In this example, `.isEmpty()` is used to check if a queue is empty before and after adding elements: |
| 39 | + |
| 40 | +```java |
| 41 | +import java.util.LinkedList; |
| 42 | +import java.util.Queue; |
| 43 | + |
| 44 | +public class Main { |
| 45 | + public static void main(String[] args) { |
| 46 | + Queue<String> queue = new LinkedList<String>(); |
| 47 | + |
| 48 | + // Check if queue is empty initially |
| 49 | + System.out.println("Is queue empty? " + queue.isEmpty()); |
| 50 | + System.out.println("Queue contents: " + queue); |
| 51 | + |
| 52 | + // Add elements to the queue |
| 53 | + queue.offer("First"); |
| 54 | + queue.offer("Second"); |
| 55 | + queue.offer("Third"); |
| 56 | + |
| 57 | + // Check if queue is empty after adding elements |
| 58 | + System.out.println("Is queue empty after adding elements? " + queue.isEmpty()); |
| 59 | + System.out.println("Queue contents: " + queue); |
| 60 | + System.out.println("Queue size: " + queue.size()); |
| 61 | + |
| 62 | + // Remove all elements |
| 63 | + queue.poll(); |
| 64 | + queue.poll(); |
| 65 | + queue.poll(); |
| 66 | + |
| 67 | + // Check if queue is empty after removing all elements |
| 68 | + System.out.println("Is queue empty after removing all elements? " + queue.isEmpty()); |
| 69 | + System.out.println("Queue contents: " + queue); |
| 70 | + } |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +The output of this code is: |
| 75 | + |
| 76 | +```shell |
| 77 | +Is queue empty? true |
| 78 | +Queue contents: [] |
| 79 | +Is queue empty after adding elements? false |
| 80 | +Queue contents: [First, Second, Third] |
| 81 | +Queue size: 3 |
| 82 | +Is queue empty after removing all elements? true |
| 83 | +Queue contents: [] |
| 84 | +``` |
| 85 | + |
| 86 | +## Example 2: Using `.isEmpty()` in a Loop |
| 87 | + |
| 88 | +This example demonstrates how `.isEmpty()` can be used to safely process all elements in a queue: |
| 89 | + |
| 90 | +```java |
| 91 | +import java.util.LinkedList; |
| 92 | +import java.util.Queue; |
| 93 | + |
| 94 | +public class Main { |
| 95 | + public static void main(String[] args) { |
| 96 | + Queue<Integer> numbers = new LinkedList<Integer>(); |
| 97 | + |
| 98 | + // Add some numbers to the queue |
| 99 | + numbers.offer(10); |
| 100 | + numbers.offer(20); |
| 101 | + numbers.offer(30); |
| 102 | + numbers.offer(40); |
| 103 | + numbers.offer(50); |
| 104 | + |
| 105 | + System.out.println("Initial queue: " + numbers); |
| 106 | + System.out.println("Processing all elements in the queue:"); |
| 107 | + |
| 108 | + int elementCount = 0; |
| 109 | + // Process all elements until queue is empty |
| 110 | + while (!numbers.isEmpty()) { |
| 111 | + Integer number = numbers.poll(); |
| 112 | + elementCount++; |
| 113 | + System.out.println("Processing element #" + elementCount + ": " + number); |
| 114 | + System.out.println("Remaining elements: " + numbers.size()); |
| 115 | + } |
| 116 | + |
| 117 | + System.out.println("Queue is now empty: " + numbers.isEmpty()); |
| 118 | + System.out.println("Total elements processed: " + elementCount); |
| 119 | + } |
| 120 | +} |
| 121 | +``` |
| 122 | + |
| 123 | +The output of this code is: |
| 124 | + |
| 125 | +```shell |
| 126 | +Initial queue: [10, 20, 30, 40, 50] |
| 127 | +Processing all elements in the queue: |
| 128 | +Processing element #1: 10 |
| 129 | +Remaining elements: 4 |
| 130 | +Processing element #2: 20 |
| 131 | +Remaining elements: 3 |
| 132 | +Processing element #3: 30 |
| 133 | +Remaining elements: 2 |
| 134 | +Processing element #4: 40 |
| 135 | +Remaining elements: 1 |
| 136 | +Processing element #5: 50 |
| 137 | +Remaining elements: 0 |
| 138 | +Queue is now empty: true |
| 139 | +Total elements processed: 5 |
| 140 | +``` |
0 commit comments