Java PriorityQueue contains() Method

The PriorityQueue class in Java provides the contains(Object o) method to check if a specific element is present in the queue.

Table of Contents

  1. Introduction
  2. contains Method Syntax
  3. Examples
    • Checking if an Element is Present in the PriorityQueue
    • Handling an Element Not Present in the PriorityQueue
  4. Real-World Use Case
    • Use Case: Task Management System
  5. Conclusion

Introduction

The PriorityQueue.contains(Object o) method is used to determine whether a specific element is present in the PriorityQueue. This method is useful when you need to verify the presence of an element without removing it from the queue.

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 type Object, which is the element to check for presence.
  • The method returns a boolean value: true if the element is present in the queue, false otherwise.

Examples

Checking if an Element is Present in the PriorityQueue

The contains method can be used to check if a specific element is present in a PriorityQueue.

Example

import java.util.PriorityQueue;

public class PriorityQueueContainsExample {
    public static void main(String[] args) {
        // Creating a PriorityQueue of Strings
        PriorityQueue<String> tasks = new PriorityQueue<>();

        // Adding elements to the PriorityQueue
        tasks.add("Complete project report");
        tasks.add("Email client updates");
        tasks.add("Prepare presentation");

        // Checking if specific elements are present in the PriorityQueue
        boolean containsEmailUpdate = tasks.contains("Email client updates");
        boolean containsTeamMeeting = tasks.contains("Team meeting");

        // Printing the results
        System.out.println("PriorityQueue contains 'Email client updates': " + containsEmailUpdate);
        System.out.println("PriorityQueue contains 'Team meeting': " + containsTeamMeeting);
    }
}

Output:

PriorityQueue contains 'Email client updates': true
PriorityQueue contains 'Team meeting': false

Handling an Element Not Present in the PriorityQueue

When the element is not present in the PriorityQueue, the contains method returns false.

Example

import java.util.PriorityQueue;

public class ElementNotPresentExample {
    public static void main(String[] args) {
        // Creating a PriorityQueue of Strings
        PriorityQueue<String> tasks = new PriorityQueue<>();

        // Adding elements to the PriorityQueue
        tasks.add("Complete project report");
        tasks.add("Email client updates");

        // Checking for an element not present in the PriorityQueue
        boolean containsNonExistentTask = tasks.contains("Non-existent task");

        // Printing the result
        System.out.println("PriorityQueue contains 'Non-existent task': " + containsNonExistentTask);
    }
}

Output:

PriorityQueue 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 queue before adding it to avoid duplication. The contains method can help achieve this functionality.

Example

import java.util.PriorityQueue;

public class TaskManagementSystem {
    public static void main(String[] args) {
        // Creating a PriorityQueue to store tasks
        PriorityQueue<Task> tasks = new PriorityQueue<>();

        // Adding initial tasks with different priorities
        tasks.add(new Task("Complete project report", 2));
        tasks.add(new Task("Email client updates", 1));

        // Checking if a task is already in the queue
        Task newTask = new Task("Complete project report", 2);
        if (tasks.contains(newTask)) {
            System.out.println("Task already exists in the queue: " + newTask);
        } else {
            tasks.add(newTask);
            System.out.println("Task added to the queue: " + newTask);
        }
    }
}

class Task implements Comparable<Task> {
    private String description;
    private int priority;

    public Task(String description, int priority) {
        this.description = description;
        this.priority = priority;
    }

    @Override
    public int compareTo(Task other) {
        return Integer.compare(this.priority, other.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 queue: Complete project report (Priority: 2)

Conclusion

The PriorityQueue.contains(Object o) method in Java is used for checking the presence of an element in a PriorityQueue without removing it. Understanding how to use this method can help manage and verify elements in the queue effectively, especially in applications like task management systems where avoiding duplication of tasks is essential.

Comments