From 0291e940a4d0367728775fc00bc4e826ea8a3e70 Mon Sep 17 00:00:00 2001 From: trigg770 Date: Thu, 10 Sep 2026 19:54:58 +0800 Subject: [PATCH] Use streams where loops only filter, map or join Four loops walk a collection only to keep some of its elements, turn each element into something else, or join the results into a string: finding tasks by keyword, looking up a command by its keyword, listing every keyword, and turning tasks into save lines. Each hides that simple intent behind an accumulator or an index that the reader has to trace. Let's write these four as streams, which name the operation directly (filter, findFirst, map, joining) and leave no accumulator to follow. Loops where a stream would not read more simply stay as they are: Storage.load also counts the lines it skips, decodeSaveField looks one character ahead, Ui.showNumbered needs each task's position, and Task.toSaveLine appends other fields around its loop. Co-Authored-By: Claude Opus 5 --- src/main/java/ted/Storage.java | 7 +++---- src/main/java/ted/command/CommandType.java | 24 ++++++++++------------ src/main/java/ted/task/TaskList.java | 10 +++------ 3 files changed, 17 insertions(+), 24 deletions(-) diff --git a/src/main/java/ted/Storage.java b/src/main/java/ted/Storage.java index b7628abe5d..2a01d32fba 100644 --- a/src/main/java/ted/Storage.java +++ b/src/main/java/ted/Storage.java @@ -70,10 +70,9 @@ public void save(TaskList tasks) throws TedException { Files.createDirectories(parentDir); } - List lines = new ArrayList<>(); - for (Task task : tasks.asList()) { - lines.add(task.toSaveFormat()); - } + List lines = tasks.asList().stream() + .map(Task::toSaveFormat) + .toList(); Files.write(dataFile, lines); } catch (IOException e) { throw new TedException("Unable to save tasks to " + dataFile diff --git a/src/main/java/ted/command/CommandType.java b/src/main/java/ted/command/CommandType.java index 07a7370a29..68450ab64b 100644 --- a/src/main/java/ted/command/CommandType.java +++ b/src/main/java/ted/command/CommandType.java @@ -1,5 +1,8 @@ package ted.command; +import java.util.Arrays; +import java.util.stream.Collectors; + import ted.TedException; /** @@ -64,14 +67,11 @@ public String getKeyword() { * @throws TedException if no command uses that keyword. */ public static CommandType fromKeyword(String keyword) throws TedException { - for (CommandType command : values()) { - if (command.keyword.equals(keyword)) { - return command; - } - } - - throw new TedException("I don't recognise \"" + keyword + "\". " - + "I understand: " + listKeywords() + "."); + return Arrays.stream(values()) + .filter(command -> command.keyword.equals(keyword)) + .findFirst() + .orElseThrow(() -> new TedException("I don't recognise \"" + keyword + "\". " + + "I understand: " + listKeywords() + ".")); } /** @@ -82,10 +82,8 @@ public static CommandType fromKeyword(String keyword) throws TedException { * @return the keywords, separated by commas. */ private static String listKeywords() { - String[] keywords = new String[values().length]; - for (int i = 0; i < values().length; i++) { - keywords[i] = values()[i].keyword; - } - return String.join(", ", keywords); + return Arrays.stream(values()) + .map(CommandType::getKeyword) + .collect(Collectors.joining(", ")); } } diff --git a/src/main/java/ted/task/TaskList.java b/src/main/java/ted/task/TaskList.java index d9f3a17753..51eab4f8b7 100644 --- a/src/main/java/ted/task/TaskList.java +++ b/src/main/java/ted/task/TaskList.java @@ -93,13 +93,9 @@ public boolean isEmpty() { */ public TaskList find(String keyword) { String lowerCaseKeyword = keyword.toLowerCase(); - TaskList matches = new TaskList(); - for (Task task : tasks) { - if (task.description.toLowerCase().contains(lowerCaseKeyword)) { - matches.add(task); - } - } - return matches; + return new TaskList(tasks.stream() + .filter(task -> task.description.toLowerCase().contains(lowerCaseKeyword)) + .toList()); } /**