Java PriorityQueue forEach() Method

The PriorityQueue class in Java provides the forEach(Consumer<? super E> action) method to perform the given action for each element in the queue.

Table of Contents

  1. Introduction
  2. forEach Method Syntax
  3. Examples
    • Using forEach to Print Elements
    • Using forEach to Perform Custom Actions
  4. Real-World Use Case
    • Use Case: Task Management System
  5. Conclusion

Introduction

The PriorityQueue.forEach(Consumer<? super E> action) method is used to iterate over each element in the PriorityQueue and perform the specified action. This method is useful for processing all elements in the queue without modifying it.

forEach Method Syntax

The syntax for the forEach method is as follows:

public void forEach(Consumer<? super E> action)
  • The method takes a single parameter action of type Consumer<? super E>, which defines the action to be performed on each element.
  • The method does not return any value.

Examples

Using forEach to Print Elements

The forEach method can be used to print all elements in a PriorityQueue.

Example

import java.util.PriorityQueue;

public class PriorityQueueForEachExample {
    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");

        // Using forEach to print all elements
        tasks.forEach(task -> System.out.println("Task: " + task));
    }
}

Output:

Task: Complete project report
Task: Email client updates
Task: Prepare presentation

Using forEach to Perform Custom Actions

The forEach method can also be used to perform custom actions on each element in the PriorityQueue.

Example

import java.util.PriorityQueue;

public class CustomActionForEachExample {
    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");

        // Using forEach to perform custom actions on all elements
        tasks.forEach(task -> {
            // Custom action: print task in uppercase
            System.out.println("Task: " + task.toUpperCase());
        });
    }
}

Output:

Task: COMPLETE PROJECT REPORT
Task: EMAIL CLIENT UPDATES
Task: PREPARE PRESENTATION

Real-World Use Case

Use Case: Task Management System

In a task management system, you might need to iterate over all tasks in the queue to perform actions such as logging, updating statuses, or sending notifications. The forEach 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));
        tasks.add(new Task("Prepare presentation", 3));

        // Using forEach to log task details
        tasks.forEach(task -> System.out.println("Logging task: " + task));
    }
}

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 String toString() {
        return description + " (Priority: " + priority + ")";
    }
}

Output:

Logging task: Email client updates (Priority: 1)
Logging task: Complete project report (Priority: 2)
Logging task: Prepare presentation (Priority: 3)

Conclusion

The PriorityQueue.forEach(Consumer<? super E> action) method in Java is used for performing actions on each element in the queue.

Comments