Code Example: Task Management Using ArrayList and Iterator

This code example illustrates the use of ArrayList and Iterator in Java for task management. It demonstrates how to create a list of tasks, display all tasks, remove completed tasks, and update the status of tasks in progress to completed.

This example emphasizes condition-based operations, showing how to traverse and modify a list dynamically using Iterator. It is a practical example of managing a dynamic list of tasks with common operations in a task management system.

Advanced Code Example: Task Management Using ArrayList and Iterator in Java

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class TaskManagement {
    public static void main(String[] args) {
        // Creating an ArrayList of Task objects
        List<Task> tasks = new ArrayList<>();
        tasks.add(new Task(1, "Write a blog post", "Pending"));
        tasks.add(new Task(2, "Fix bugs in the code", "In Progress"));
        tasks.add(new Task(3, "Prepare for meeting", "Completed"));
        tasks.add(new Task(4, "Review pull requests", "Pending"));
        tasks.add(new Task(5, "Update documentation", "In Progress"));

        // Displaying all tasks
        System.out.println("All Tasks:");
        displayTasks(tasks);

        // Removing completed tasks
        System.out.println("\nRemoving completed tasks:");
        removeCompletedTasks(tasks);
        displayTasks(tasks);

        // Updating task status
        System.out.println("\nUpdating task status to 'Completed' for tasks in progress:");
        updateTaskStatus(tasks, "In Progress", "Completed");
        displayTasks(tasks);
    }

    // Method to display tasks
    public static void displayTasks(List<Task> tasks) {
        for (Task task : tasks) {
            System.out.println(task);
        }
    }

    // Method to remove completed tasks
    public static void removeCompletedTasks(List<Task> tasks) {
        Iterator<Task> iterator = tasks.iterator();
        while (iterator.hasNext()) {
            Task task = iterator.next();
            if (task.getStatus().equals("Completed")) {
                iterator.remove();
            }
        }
    }

    // Method to update task status
    public static void updateTaskStatus(List<Task> tasks, String oldStatus, String newStatus) {
        Iterator<Task> iterator = tasks.iterator();
        while (iterator.hasNext()) {
            Task task = iterator.next();
            if (task.getStatus().equals(oldStatus)) {
                task.setStatus(newStatus);
            }
        }
    }
}

// Task class representing a task
class Task {
    private int id;
    private String description;
    private String status;

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

    public int getId() {
        return id;
    }

    public String getDescription() {
        return description;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    @Override
    public String toString() {
        return "Task{id=" + id + ", description='" + description + "', status='" + status + "'}";
    }
}

Explanation

Task Class

  • Task Class: Represents a task with fields for ID, description, and status. Includes getter and setter methods and an overridden toString() method for easy printing.

Main Class (TaskManagement)

  • Creating ArrayList: Initializes an ArrayList of Task objects with sample data.
  • Display Tasks: Uses the displayTasks method to print all tasks.
  • Removing Completed Tasks: Uses the removeCompletedTasks method to remove tasks with the status “Completed” from the list.
  • Updating Task Status: Uses the updateTaskStatus method to change the status of tasks from “In Progress” to “Completed”.

Methods

  • displayTasks: Iterates through the list and prints each task.
  • removeCompletedTasks: Iterates through the list using an Iterator and removes tasks with the status “Completed”.
  • updateTaskStatus: Iterates through the list using an Iterator and updates the status of tasks based on a condition.

Key Takeaways

  • Advanced Use of ArrayList and Iterator: Demonstrates managing a dynamic list of tasks with adding, removing, and updating operations.
  • Condition-Based Operations: Shows how to perform operations on the list based on specific conditions.
  • Enhanced Readability and Reusability: Uses well-defined methods to encapsulate logic for better readability and reusability.