📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
The ArrayDeque
class in Java provides the contains(Object o)
method to check if a specific element is present in the deque.
Table of Contents
- Introduction
contains
Method Syntax- Examples
- Checking if an Element is Present in the ArrayDeque
- Handling an Element Not Present in the ArrayDeque
- Real-World Use Case
- Use Case: Task Management System
- Conclusion
Introduction
The ArrayDeque.contains(Object o)
method is used to determine whether a specific element is present in the ArrayDeque
. This method is useful when you need to verify the presence of an element without removing it from the deque.
contains Method Syntax
The syntax for the contains
method is as follows:
public boolean contains(Object o)
- The method takes a single parameter
o
of typeObject
, which is the element to check for presence. - The method returns a boolean value:
true
if the element is present in the deque,false
otherwise.
Examples
Checking if an Element is Present in the ArrayDeque
The contains
method can be used to check if a specific element is present in an ArrayDeque
.
Example
import java.util.ArrayDeque; public class ArrayDequeContainsExample { public static void main(String[] args) { // Creating an ArrayDeque of Strings ArrayDeque<String> tasks = new ArrayDeque<>(); // Adding elements to the ArrayDeque tasks.add("Complete project report"); tasks.add("Email client updates"); tasks.add("Prepare presentation"); // Checking if specific elements are present in the ArrayDeque boolean containsEmailUpdate = tasks.contains("Email client updates"); boolean containsTeamMeeting = tasks.contains("Team meeting"); // Printing the results System.out.println("ArrayDeque contains 'Email client updates': " + containsEmailUpdate); System.out.println("ArrayDeque contains 'Team meeting': " + containsTeamMeeting); } }
Output:
ArrayDeque contains 'Email client updates': true ArrayDeque contains 'Team meeting': false
Handling an Element Not Present in the ArrayDeque
When the element is not present in the ArrayDeque
, the contains
method returns false
.
Example
import java.util.ArrayDeque; public class ElementNotPresentExample { public static void main(String[] args) { // Creating an ArrayDeque of Strings ArrayDeque<String> tasks = new ArrayDeque<>(); // Adding elements to the ArrayDeque tasks.add("Complete project report"); tasks.add("Email client updates"); // Checking for an element not present in the ArrayDeque boolean containsNonExistentTask = tasks.contains("Non-existent task"); // Printing the result System.out.println("ArrayDeque contains 'Non-existent task': " + containsNonExistentTask); } }
Output:
ArrayDeque contains 'Non-existent task': false
Real-World Use Case
Use Case: Task Management System
In a task management system, you may need to check if a specific task is already in the deque before adding it to avoid duplication. The contains
method can help achieve this functionality.
Example
import java.util.ArrayDeque; public class TaskManagementSystem { public static void main(String[] args) { // Creating an ArrayDeque to store tasks ArrayDeque<Task> tasks = new ArrayDeque<>(); // Adding initial tasks tasks.add(new Task("Complete project report", 2)); tasks.add(new Task("Email client updates", 1)); // Checking if a task is already in the deque Task newTask = new Task("Complete project report", 2); if (tasks.contains(newTask)) { System.out.println("Task already exists in the deque: " + newTask); } else { tasks.add(newTask); System.out.println("Task added to the deque: " + newTask); } } } class Task { private String description; private int priority; public Task(String description, int priority) { this.description = description; this.priority = priority; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Task task = (Task) o; if (priority != task.priority) return false; return description != null ? description.equals(task.description) : task.description == null; } @Override public int hashCode() { int result = description != null ? description.hashCode() : 0; result = 31 * result + priority; return result; } @Override public String toString() { return description + " (Priority: " + priority + ")"; } }
Output:
Task already exists in the deque: Complete project report (Priority: 2)
Conclusion
The ArrayDeque.contains(Object o)
method in Java is used for checking the presence of an element in a deque without removing it. Understanding how to use this method can help manage and verify elements in the deque effectively, especially in applications like task management systems where avoiding duplication of tasks is essential.
Comments
Post a Comment
Leave Comment