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.
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 + "'}";
}
}
toString()
method for easy printing.ArrayList
of Task
objects with sample data.displayTasks
method to print all tasks.removeCompletedTasks
method to remove tasks with the status “Completed” from the list.updateTaskStatus
method to change the status of tasks from “In Progress” to “Completed”.Iterator
and removes tasks with the status “Completed”.Iterator
and updates the status of tasks based on a condition.