From 28fe5f795d3ddc842d1b27f0d042765152cd773d Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Fri, 29 Aug 2025 17:18:33 +0530 Subject: [PATCH 1/7] [Term Entry] Java Queue: clear() --- .../java/concepts/queue/terms/clear/clear.md | 227 ++++++++++++++++++ 1 file changed, 227 insertions(+) create mode 100644 content/java/concepts/queue/terms/clear/clear.md diff --git a/content/java/concepts/queue/terms/clear/clear.md b/content/java/concepts/queue/terms/clear/clear.md new file mode 100644 index 00000000000..7f7190ab653 --- /dev/null +++ b/content/java/concepts/queue/terms/clear/clear.md @@ -0,0 +1,227 @@ +--- +Title: 'clear()' +Description: 'Removes all elements from a Java Queue making it empty.' +Subjects: + - 'Code Foundations' + - 'Computer Science' +Tags: + - 'Collections' + - 'Data Structures' + - 'Methods' + - 'Queues' +CatalogContent: + - 'learn-java' + - 'paths/computer-science' +--- + +The **`clear()`** method in Java Queue removes all elements from the queue, making it completely empty. This method is inherited from the Collection interface and provides a convenient way to reset a queue without creating a new instance. + +## Syntax + +```pseudo +queueName.clear() +``` + +**Parameters:** + +- This method does not take any parameters. + +**Return value:** + +- The method does not return any value (`void`). + +## Example 1: Basic Queue Clear + +This example demonstrates the fundamental usage of the `clear()` method with a LinkedList implementation of Queue: + +```java +import java.util.LinkedList; +import java.util.Queue; + +public class QueueClearBasic { + public static void main(String[] args) { + // Create a queue using LinkedList implementation + Queue queue = new LinkedList<>(); + + // Add elements to the queue + queue.offer("First"); + queue.offer("Second"); + queue.offer("Third"); + queue.offer("Fourth"); + + // Display the original queue + System.out.println("Original queue: " + queue); + System.out.println("Queue size before clear: " + queue.size()); + + // Clear all elements from the queue + queue.clear(); + + // Display the queue after clearing + System.out.println("Queue after clear: " + queue); + System.out.println("Queue size after clear: " + queue.size()); + } +} +``` + +The output of this code is: + +```shell +Original queue: [First, Second, Third, Fourth] +Queue size before clear: 4 +Queue after clear: [] +Queue size after clear: 0 +``` + +This example creates a queue, adds four string elements, displays the original content, clears the queue, and then shows the empty result. The `clear()` method removes all elements but keeps the queue structure intact for future use. + +## Example 2: Task Processing System + +This example shows how `clear()` can be used in a task processing system where you need to reset the task queue after completing a batch of operations: + +```java +import java.util.LinkedList; +import java.util.Queue; + +public class TaskProcessingSystem { + public static void main(String[] args) { + // Create a task queue + Queue taskQueue = new LinkedList<>(); + + // Add tasks to the queue + taskQueue.offer("Process Payment"); + taskQueue.offer("Send Email"); + taskQueue.offer("Update Database"); + taskQueue.offer("Generate Report"); + taskQueue.offer("Backup Data"); + + System.out.println("Tasks in queue: " + taskQueue); + System.out.println("Total tasks: " + taskQueue.size()); + + // Process all tasks + while (!taskQueue.isEmpty()) { + String currentTask = taskQueue.poll(); + System.out.println("Processing: " + currentTask); + } + + System.out.println("All tasks completed. Queue status: " + taskQueue); + + // Add new batch of tasks + taskQueue.offer("Daily Cleanup"); + taskQueue.offer("System Maintenance"); + + System.out.println("New tasks added: " + taskQueue); + + // Clear the queue for emergency reset + System.out.println("Emergency reset triggered!"); + taskQueue.clear(); + + System.out.println("Queue after emergency clear: " + taskQueue); + System.out.println("Ready for new tasks: " + taskQueue.isEmpty()); + } +} +``` + +The output of this code is: + +```shell +Tasks in queue: [Process Payment, Send Email, Update Database, Generate Report, Backup Data] +Total tasks: 5 +Processing: Process Payment +Processing: Send Email +Processing: Update Database +Processing: Generate Report +Processing: Backup Data +All tasks completed. Queue status: [] +New tasks added: [Daily Cleanup, System Maintenance] +Emergency reset triggered! +Queue after emergency clear: [] +Ready for new tasks: true +``` + +This example demonstrates a realistic scenario where a task processing system uses `clear()` for emergency resets or batch completions. The method provides a quick way to empty the queue without affecting its functionality. + +## Example 3: Cache Management System + +This example illustrates how `clear()` is useful in cache management systems where periodic cache clearing is necessary for memory optimization: + +```java +import java.util.PriorityQueue; +import java.util.Queue; + +public class CacheManagementSystem { + public static void main(String[] args) { + // Create a priority queue for cache management + Queue cacheQueue = new PriorityQueue<>(); + + // Simulate adding cache entries with priority values + cacheQueue.offer(10); // Low priority + cacheQueue.offer(5); // High priority + cacheQueue.offer(15); // Lower priority + cacheQueue.offer(3); // Highest priority + cacheQueue.offer(12); // Medium priority + + System.out.println("Cache entries (priority order): " + cacheQueue); + System.out.println("Cache size: " + cacheQueue.size()); + System.out.println("Highest priority item: " + cacheQueue.peek()); + + // Simulate cache usage + System.out.println("\nProcessing cache entries:"); + Queue tempQueue = new PriorityQueue<>(cacheQueue); + while (!tempQueue.isEmpty()) { + System.out.println("Accessing cache entry: " + tempQueue.poll()); + } + + // Check memory usage and clear cache if needed + boolean memoryThresholdExceeded = true; // Simulated condition + + if (memoryThresholdExceeded) { + System.out.println("\nMemory threshold exceeded. Clearing cache..."); + cacheQueue.clear(); + System.out.println("Cache cleared successfully."); + System.out.println("Current cache size: " + cacheQueue.size()); + System.out.println("Cache is empty: " + cacheQueue.isEmpty()); + + // Cache is now ready for new entries + System.out.println("\nCache system ready for new entries."); + } + } +} +``` + +The output of this code is: + +```shell +Cache entries (priority order): [3, 5, 15, 10, 12] +Cache size: 5 +Highest priority item: 3 + +Processing cache entries: +Accessing cache entry: 3 +Accessing cache entry: 5 +Accessing cache entry: 10 +Accessing cache entry: 12 +Accessing cache entry: 15 + +Memory threshold exceeded. Clearing cache... +Cache cleared successfully. +Current cache size: 0 +Cache is empty: true + +Cache system ready for new entries. +``` + +This example shows how `clear()` method works with different Queue implementations like PriorityQueue. The method is particularly useful in cache management systems where you need to periodically clear cached data to free up memory. + +## Frequently Asked Questions + +### 1. Does the `clear()` method destroy the queue object? + +No, the `clear()` method only removes all elements from the queue but preserves the queue structure. You can continue using the same queue instance after clearing it. + +### 2. What is the time complexity of the `clear()` method? + +The time complexity is typically O(n) where n is the number of elements in the queue, as it needs to remove each element. However, some implementations may optimize this operation. + +### 3. Can I use `clear()` on thread-safe queue implementations? + +Yes, the `clear()` method works with thread-safe queue implementations like `LinkedBlockingQueue` and `ConcurrentLinkedQueue`. The operation remains thread-safe in these implementations. From d51973c14a5304889651bf4def3b31da1cceda05 Mon Sep 17 00:00:00 2001 From: Avdhoot <50920321+avdhoottt@users.noreply.github.com> Date: Tue, 28 Oct 2025 14:33:35 +0530 Subject: [PATCH 2/7] Update content/java/concepts/queue/terms/clear/clear.md --- content/java/concepts/queue/terms/clear/clear.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/java/concepts/queue/terms/clear/clear.md b/content/java/concepts/queue/terms/clear/clear.md index 7f7190ab653..2a7fbe9f22d 100644 --- a/content/java/concepts/queue/terms/clear/clear.md +++ b/content/java/concepts/queue/terms/clear/clear.md @@ -14,7 +14,7 @@ CatalogContent: - 'paths/computer-science' --- -The **`clear()`** method in Java Queue removes all elements from the queue, making it completely empty. This method is inherited from the Collection interface and provides a convenient way to reset a queue without creating a new instance. +The **`clear()`** method in Java [Queue](https://www.codecademy.com/resources/docs/java/queue) removes all elements from the queue, making it completely empty. This method is inherited from the [Collection](https://www.codecademy.com/resources/docs/java/collection) interface and provides a convenient way to reset a queue without creating a new instance. ## Syntax From c973ed6d28f2a92fed9e2e478d67ad85d20dfb9c Mon Sep 17 00:00:00 2001 From: Avdhoot <50920321+avdhoottt@users.noreply.github.com> Date: Tue, 28 Oct 2025 14:34:12 +0530 Subject: [PATCH 3/7] Update content/java/concepts/queue/terms/clear/clear.md --- content/java/concepts/queue/terms/clear/clear.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/java/concepts/queue/terms/clear/clear.md b/content/java/concepts/queue/terms/clear/clear.md index 2a7fbe9f22d..8b471c17a92 100644 --- a/content/java/concepts/queue/terms/clear/clear.md +++ b/content/java/concepts/queue/terms/clear/clear.md @@ -32,7 +32,7 @@ queueName.clear() ## Example 1: Basic Queue Clear -This example demonstrates the fundamental usage of the `clear()` method with a LinkedList implementation of Queue: +This example demonstrates the fundamental usage of the `clear()` method with a [LinkedList](https://www.codecademy.com/resources/docs/java/linked-list) implementation of Queue: ```java import java.util.LinkedList; From 5821fbd227adffd7bc54450414d9f9887f6dde62 Mon Sep 17 00:00:00 2001 From: Avdhoot <50920321+avdhoottt@users.noreply.github.com> Date: Tue, 28 Oct 2025 14:34:52 +0530 Subject: [PATCH 4/7] Update content/java/concepts/queue/terms/clear/clear.md --- content/java/concepts/queue/terms/clear/clear.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/java/concepts/queue/terms/clear/clear.md b/content/java/concepts/queue/terms/clear/clear.md index 8b471c17a92..a1bbb33b383 100644 --- a/content/java/concepts/queue/terms/clear/clear.md +++ b/content/java/concepts/queue/terms/clear/clear.md @@ -76,7 +76,7 @@ This example creates a queue, adds four string elements, displays the original c ## Example 2: Task Processing System -This example shows how `clear()` can be used in a task processing system where you need to reset the task queue after completing a batch of operations: +This example illustrates how `clear()` can be utilized in a task processing system to reset the task queue after completing a batch of operations. ```java import java.util.LinkedList; From 97146a56e001a8e6ba8f21e076d28df24980f5fd Mon Sep 17 00:00:00 2001 From: Avdhoot <50920321+avdhoottt@users.noreply.github.com> Date: Tue, 28 Oct 2025 14:35:09 +0530 Subject: [PATCH 5/7] Update content/java/concepts/queue/terms/clear/clear.md --- content/java/concepts/queue/terms/clear/clear.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/java/concepts/queue/terms/clear/clear.md b/content/java/concepts/queue/terms/clear/clear.md index a1bbb33b383..845ff50795c 100644 --- a/content/java/concepts/queue/terms/clear/clear.md +++ b/content/java/concepts/queue/terms/clear/clear.md @@ -76,7 +76,7 @@ This example creates a queue, adds four string elements, displays the original c ## Example 2: Task Processing System -This example illustrates how `clear()` can be utilized in a task processing system to reset the task queue after completing a batch of operations. +This example illustrates how `clear()` can be utilized in a task processing system to reset the task queue after completing a batch of operations: ```java import java.util.LinkedList; From 228f1bddc0b7d45ad9b7108429159c1ec8de6e4f Mon Sep 17 00:00:00 2001 From: Avdhoot <50920321+avdhoottt@users.noreply.github.com> Date: Tue, 28 Oct 2025 14:36:26 +0530 Subject: [PATCH 6/7] Update content/java/concepts/queue/terms/clear/clear.md --- content/java/concepts/queue/terms/clear/clear.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/content/java/concepts/queue/terms/clear/clear.md b/content/java/concepts/queue/terms/clear/clear.md index 845ff50795c..148e02587a6 100644 --- a/content/java/concepts/queue/terms/clear/clear.md +++ b/content/java/concepts/queue/terms/clear/clear.md @@ -210,7 +210,7 @@ Cache is empty: true Cache system ready for new entries. ``` -This example shows how `clear()` method works with different Queue implementations like PriorityQueue. The method is particularly useful in cache management systems where you need to periodically clear cached data to free up memory. +This example shows how `clear()` method works with different Queue implementations like [PriorityQueue](https://www.codecademy.com/resources/docs/java/priorityqueue). The method is particularly useful in cache management systems where you need to periodically clear cached data to free up memory. ## Frequently Asked Questions @@ -220,7 +220,7 @@ No, the `clear()` method only removes all elements from the queue but preserves ### 2. What is the time complexity of the `clear()` method? -The time complexity is typically O(n) where n is the number of elements in the queue, as it needs to remove each element. However, some implementations may optimize this operation. +The time complexity is typically `O(n)` where n is the number of elements in the queue, as it needs to remove each element. However, some implementations may optimize this operation. ### 3. Can I use `clear()` on thread-safe queue implementations? From db926b6d6059b418037c76c733413e5704382a8f Mon Sep 17 00:00:00 2001 From: Avdhoot <50920321+avdhoottt@users.noreply.github.com> Date: Tue, 28 Oct 2025 14:37:37 +0530 Subject: [PATCH 7/7] Update content/java/concepts/queue/terms/clear/clear.md --- content/java/concepts/queue/terms/clear/clear.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/java/concepts/queue/terms/clear/clear.md b/content/java/concepts/queue/terms/clear/clear.md index 148e02587a6..7c1d5fb4a71 100644 --- a/content/java/concepts/queue/terms/clear/clear.md +++ b/content/java/concepts/queue/terms/clear/clear.md @@ -210,7 +210,7 @@ Cache is empty: true Cache system ready for new entries. ``` -This example shows how `clear()` method works with different Queue implementations like [PriorityQueue](https://www.codecademy.com/resources/docs/java/priorityqueue). The method is particularly useful in cache management systems where you need to periodically clear cached data to free up memory. +The following text explains how the `clear()` method works with different Queue implementations, such as [PriorityQueue](https://www.codecademy.com/resources/docs/java/priorityqueue). This method is particularly useful in cache management systems where there is a need to periodically clear cached data to free up memory. ## Frequently Asked Questions