From 254a7022b6fe3ae1075b1f259879574610628380 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Sun, 24 Aug 2025 13:41:17 +0530 Subject: [PATCH 1/7] [Term Entry] Java Queue: contains() --- .../concepts/queue/terms/contains/contains.md | 231 ++++++++++++++++++ 1 file changed, 231 insertions(+) create mode 100644 content/java/concepts/queue/terms/contains/contains.md diff --git a/content/java/concepts/queue/terms/contains/contains.md b/content/java/concepts/queue/terms/contains/contains.md new file mode 100644 index 00000000000..d6963517925 --- /dev/null +++ b/content/java/concepts/queue/terms/contains/contains.md @@ -0,0 +1,231 @@ +--- +Title: 'contains()' +Description: 'Checks whether a specific element is present in a Java Queue and returns a boolean value indicating the result' +Subjects: + - 'Computer Science' + - 'Data Science' +Tags: + - 'Boolean' + - 'Collections' + - 'Methods' + - 'Queues' +CatalogContent: + - 'learn-java' + - 'paths/computer-science' +--- + +The **`contains()`** method is a built-in [method](https://www.codecademy.com/resources/docs/java/methods) in Java's Queue interface that checks whether a specific element is present in the queue. This method returns `true` if the queue contains the specified element, and `false` otherwise. The `contains()` method is inherited from the [Collection](https://www.codecademy.com/resources/docs/java/collection) interface and provides an efficient way to verify the presence of elements without modifying the queue structure. + +## Syntax + +```pseudo +boolean contains(Object element) +``` + +**Parameters:** + +- `element`: The element whose presence in the queue is to be checked + +**Return value:** + +The method returns a boolean value: + +- `true` if the queue contains the specified element +- `false` if the element is not found in the queue + +## Example 1: Basic `contains()` Usage in Java + +This example demonstrates the fundamental usage of the `contains()` method with a LinkedList queue implementation: + +```java +import java.util.LinkedList; +import java.util.Queue; + +public class QueueContainsExample { + public static void main(String[] args) { + // Create a queue using LinkedList + Queue queue = new LinkedList<>(); + + // Add elements to the queue + queue.add("Apple"); + queue.add("Banana"); + queue.add("Cherry"); + + // Check if queue contains specific elements + boolean hasApple = queue.contains("Apple"); + boolean hasGrape = queue.contains("Grape"); + + System.out.println("Queue contains Apple: " + hasApple); + System.out.println("Queue contains Grape: " + hasGrape); + } +} +``` + +The output of this code is: + +```shell +Queue contains Apple: true +Queue contains Grape: false +``` + +This example creates a queue with fruit names and demonstrates how `contains()` returns `true` for existing elements and `false` for non-existing elements. + +## Example 2: Customer Order Processing with `contains()` + +This example shows how the `contains()` method can be used in a real-world customer order processing system to check if specific orders are pending: + +```java +import java.util.LinkedList; +import java.util.Queue; + +public class OrderProcessor { + public static void main(String[] args) { + // Create a queue to store pending order IDs + Queue pendingOrders = new LinkedList<>(); + + // Add some order IDs to the queue + pendingOrders.add(1001); + pendingOrders.add(1002); + pendingOrders.add(1003); + pendingOrders.add(1004); + + // Check if specific orders are in the pending queue + int customerOrder = 1002; + if (pendingOrders.contains(customerOrder)) { + System.out.println("Order " + customerOrder + " is currently pending"); + System.out.println("Estimated position in queue: " + getOrderPosition(pendingOrders, customerOrder)); + } else { + System.out.println("Order " + customerOrder + " is not in the pending queue"); + } + + // Check another order + int anotherOrder = 1005; + if (pendingOrders.contains(anotherOrder)) { + System.out.println("Order " + anotherOrder + " is currently pending"); + } else { + System.out.println("Order " + anotherOrder + " has been processed or doesn't exist"); + } + } + + // Helper method to find position of order in queue + private static int getOrderPosition(Queue queue, int orderId) { + int position = 1; + for (Integer order : queue) { + if (order.equals(orderId)) { + return position; + } + position++; + } + return -1; // Not found + } +} +``` + +The output of this code is: + +```shell +Order 1002 is currently pending +Estimated position in queue: 2 +Order 1005 has been processed or doesn't exist +``` + +This example demonstrates how businesses can use the `contains()` method to quickly verify order status and provide customers with accurate information about their pending orders. + +## Example 3: Task Management System with `contains()` + +This example illustrates using the `contains()` method in a task management system where different priority tasks are queued for execution: + +```java +import java.util.PriorityQueue; +import java.util.Queue; + +class Task implements Comparable { + private String name; + private int priority; + + public Task(String name, int priority) { + this.name = name; + this.priority = priority; + } + + @Override + public int compareTo(Task other) { + return Integer.compare(this.priority, other.priority); // Lower number = higher priority + } + + @Override + public boolean equals(Object obj) { + if (this == obj) return true; + if (obj == null || getClass() != obj.getClass()) return false; + Task task = (Task) obj; + return name.equals(task.name); + } + + @Override + public String toString() { + return name + " (Priority: " + priority + ")"; + } +} + +public class TaskManager { + public static void main(String[] args) { + // Create a priority queue for task management + Queue taskQueue = new PriorityQueue<>(); + + // Add tasks with different priorities + taskQueue.add(new Task("Database Backup", 1)); + taskQueue.add(new Task("Send Email Report", 3)); + taskQueue.add(new Task("Update Security Patches", 1)); + taskQueue.add(new Task("Clean Temp Files", 5)); + + // Check if specific tasks are scheduled + Task searchTask1 = new Task("Database Backup", 1); + Task searchTask2 = new Task("Generate Analytics", 2); + + if (taskQueue.contains(searchTask1)) { + System.out.println("Database Backup is scheduled for execution"); + } + + if (taskQueue.contains(searchTask2)) { + System.out.println("Generate Analytics is scheduled for execution"); + } else { + System.out.println("Generate Analytics is not in the task queue"); + } + + // Display current queue status + System.out.println("\nCurrent tasks in queue:"); + for (Task task : taskQueue) { + System.out.println("- " + task); + } + } +} +``` + +The output of this code is: + +```shell +Database Backup is scheduled for execution +Generate Analytics is not in the task queue + +Current tasks in queue: +- Database Backup (Priority: 1) +- Update Security Patches (Priority: 1) +- Send Email Report (Priority: 3) +- Clean Temp Files (Priority: 5) +``` + +This example shows how the `contains()` method can be used in system administration scenarios to verify whether critical tasks are queued for execution, helping administrators track and manage automated processes. + +## Frequently Asked Questions + +### 1. What is string contains() in Java? + +The `contains()` method for queues works with any object type, including strings. When used with String elements in a queue, it checks if a specific string value exists in the queue using the `equals()` method for comparison. + +### 2. What is contains in priority queue in Java? + +In a PriorityQueue, the `contains()` method searches through the internal heap structure to find the specified element. It returns `true` if the element exists regardless of its position in the priority ordering. The time complexity is O(n) since it may need to check multiple elements. + +### 3. Is Java priority queue max or min? + +By default, Java's PriorityQueue is a min-heap, meaning the smallest element (according to natural ordering or provided Comparator) has the highest priority and is removed first. To create a max-heap, you need to provide a reverse Comparator or implement Comparable in reverse order. From 905d7c6b3f82d8ce28910071bd5875adbaf80bf1 Mon Sep 17 00:00:00 2001 From: Avdhoot <50920321+avdhoottt@users.noreply.github.com> Date: Tue, 26 Aug 2025 23:35:23 +0530 Subject: [PATCH 2/7] Update content/java/concepts/queue/terms/contains/contains.md --- content/java/concepts/queue/terms/contains/contains.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/java/concepts/queue/terms/contains/contains.md b/content/java/concepts/queue/terms/contains/contains.md index d6963517925..8d5450a2860 100644 --- a/content/java/concepts/queue/terms/contains/contains.md +++ b/content/java/concepts/queue/terms/contains/contains.md @@ -14,7 +14,7 @@ CatalogContent: - 'paths/computer-science' --- -The **`contains()`** method is a built-in [method](https://www.codecademy.com/resources/docs/java/methods) in Java's Queue interface that checks whether a specific element is present in the queue. This method returns `true` if the queue contains the specified element, and `false` otherwise. The `contains()` method is inherited from the [Collection](https://www.codecademy.com/resources/docs/java/collection) interface and provides an efficient way to verify the presence of elements without modifying the queue structure. +The **`contains()`** method in Java's [Queue](https://www.codecademy.com/resources/docs/java/queue) interface that checks whether a specific element is present in the queue. This method returns `true` if the queue contains the specified element, and `false` otherwise. The `contains()` method is inherited from the [Collection](https://www.codecademy.com/resources/docs/java/collection) interface and provides an efficient way to verify the presence of elements without modifying the queue structure. ## Syntax From 2a05f47e8a3be7209d2eb4e5dc6a09d50c57858d Mon Sep 17 00:00:00 2001 From: Avdhoot <50920321+avdhoottt@users.noreply.github.com> Date: Tue, 26 Aug 2025 23:41:58 +0530 Subject: [PATCH 3/7] Update content/java/concepts/queue/terms/contains/contains.md --- content/java/concepts/queue/terms/contains/contains.md | 1 + 1 file changed, 1 insertion(+) diff --git a/content/java/concepts/queue/terms/contains/contains.md b/content/java/concepts/queue/terms/contains/contains.md index 8d5450a2860..e6e2238fc22 100644 --- a/content/java/concepts/queue/terms/contains/contains.md +++ b/content/java/concepts/queue/terms/contains/contains.md @@ -214,6 +214,7 @@ Current tasks in queue: - Clean Temp Files (Priority: 5) ``` +> **Note:** `PriorityQueue` iteration does not guarantee elements will appear in priority order - the displayed task sequence may vary between program runs due to internal heap storage organization. This example shows how the `contains()` method can be used in system administration scenarios to verify whether critical tasks are queued for execution, helping administrators track and manage automated processes. ## Frequently Asked Questions From 2953484273a2acc1a698eeb76f4b22ef88ee6be8 Mon Sep 17 00:00:00 2001 From: Avdhoot <50920321+avdhoottt@users.noreply.github.com> Date: Tue, 26 Aug 2025 23:44:02 +0530 Subject: [PATCH 4/7] Update content/java/concepts/queue/terms/contains/contains.md --- content/java/concepts/queue/terms/contains/contains.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/content/java/concepts/queue/terms/contains/contains.md b/content/java/concepts/queue/terms/contains/contains.md index e6e2238fc22..10c1ceb83f2 100644 --- a/content/java/concepts/queue/terms/contains/contains.md +++ b/content/java/concepts/queue/terms/contains/contains.md @@ -24,14 +24,14 @@ boolean contains(Object element) **Parameters:** -- `element`: The element whose presence in the queue is to be checked +- `element`: The element whose presence in the queue is to be checked. **Return value:** The method returns a boolean value: -- `true` if the queue contains the specified element -- `false` if the element is not found in the queue +- `true` if the queue contains the specified element. +- `false` if the element is not found in the queue. ## Example 1: Basic `contains()` Usage in Java From fdc77c3e857b520f09dedbff52fa3ca96dc5ea82 Mon Sep 17 00:00:00 2001 From: Avdhoot <50920321+avdhoottt@users.noreply.github.com> Date: Tue, 26 Aug 2025 23:44:29 +0530 Subject: [PATCH 5/7] Update content/java/concepts/queue/terms/contains/contains.md --- content/java/concepts/queue/terms/contains/contains.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/java/concepts/queue/terms/contains/contains.md b/content/java/concepts/queue/terms/contains/contains.md index 10c1ceb83f2..e3fed2aae45 100644 --- a/content/java/concepts/queue/terms/contains/contains.md +++ b/content/java/concepts/queue/terms/contains/contains.md @@ -35,7 +35,7 @@ The method returns a boolean value: ## Example 1: Basic `contains()` Usage in Java -This example demonstrates the fundamental usage of the `contains()` method with a LinkedList queue implementation: +This example demonstrates the fundamental usage of the `contains()` method with a `LinkedList` queue implementation: ```java import java.util.LinkedList; From 8695471dc6f9970943590fb87900fc348a93c27c Mon Sep 17 00:00:00 2001 From: Avdhoot <50920321+avdhoottt@users.noreply.github.com> Date: Tue, 26 Aug 2025 23:48:18 +0530 Subject: [PATCH 6/7] Update content/java/concepts/queue/terms/contains/contains.md --- content/java/concepts/queue/terms/contains/contains.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/content/java/concepts/queue/terms/contains/contains.md b/content/java/concepts/queue/terms/contains/contains.md index e3fed2aae45..70c8959099e 100644 --- a/content/java/concepts/queue/terms/contains/contains.md +++ b/content/java/concepts/queue/terms/contains/contains.md @@ -215,18 +215,18 @@ Current tasks in queue: ``` > **Note:** `PriorityQueue` iteration does not guarantee elements will appear in priority order - the displayed task sequence may vary between program runs due to internal heap storage organization. + This example shows how the `contains()` method can be used in system administration scenarios to verify whether critical tasks are queued for execution, helping administrators track and manage automated processes. ## Frequently Asked Questions -### 1. What is string contains() in Java? - +### 1. What is the `contains()` method in Java? The `contains()` method for queues works with any object type, including strings. When used with String elements in a queue, it checks if a specific string value exists in the queue using the `equals()` method for comparison. -### 2. What is contains in priority queue in Java? +### 2. What is `contains()` in a priority queue in Java? -In a PriorityQueue, the `contains()` method searches through the internal heap structure to find the specified element. It returns `true` if the element exists regardless of its position in the priority ordering. The time complexity is O(n) since it may need to check multiple elements. +In a `PriorityQueue`, the `contains()` method searches through the internal heap structure to find the specified element. It returns `true` if the element exists, regardless of its position in the priority ordering. The time complexity is `O(n)` since it may need to check multiple elements. ### 3. Is Java priority queue max or min? -By default, Java's PriorityQueue is a min-heap, meaning the smallest element (according to natural ordering or provided Comparator) has the highest priority and is removed first. To create a max-heap, you need to provide a reverse Comparator or implement Comparable in reverse order. +By default, Java's `PriorityQueue` is a min-heap, meaning the smallest element (according to natural ordering or provided Comparator) has the highest priority and is removed first. To create a max-heap, you need to provide a reverse Comparator or implement Comparable in reverse order. From cad734dc5e92f0c1a3a2b938bc347e970674f8a1 Mon Sep 17 00:00:00 2001 From: avdhoottt Date: Tue, 28 Oct 2025 14:23:29 +0530 Subject: [PATCH 7/7] Format + lint --- content/java/concepts/queue/terms/contains/contains.md | 1 + 1 file changed, 1 insertion(+) diff --git a/content/java/concepts/queue/terms/contains/contains.md b/content/java/concepts/queue/terms/contains/contains.md index 70c8959099e..d4e77588637 100644 --- a/content/java/concepts/queue/terms/contains/contains.md +++ b/content/java/concepts/queue/terms/contains/contains.md @@ -221,6 +221,7 @@ This example shows how the `contains()` method can be used in system administrat ## Frequently Asked Questions ### 1. What is the `contains()` method in Java? + The `contains()` method for queues works with any object type, including strings. When used with String elements in a queue, it checks if a specific string value exists in the queue using the `equals()` method for comparison. ### 2. What is `contains()` in a priority queue in Java?