Java ArrayDeque forEach() Method

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

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 ArrayDeque.forEach(Consumer<? super E> action) method is used to iterate over each element in the ArrayDeque and perform the specified action. This method is useful for processing all elements in the deque 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 an ArrayDeque.

Example

import java.util.ArrayDeque;

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

        // 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 ArrayDeque.

Example

import java.util.ArrayDeque;

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

        // 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 deque to perform actions such as logging, updating statuses, or sending notifications. The forEach 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 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 {
    private String description;
    private int priority;

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

    @Override
    public String toString() {
        return description + " (Priority: " + priority + ")";
    }
}

Output:

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

Conclusion

The ArrayDeque.forEach(Consumer<? super E> action) method in Java is a versatile tool for performing actions on each element in the deque. Understanding how to use this method allows you to efficiently process and manage elements in the deque, making it particularly useful in applications like task management systems where you need to iterate over tasks and perform specific actions on them.

Comments