Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Use streams #4

Merged
merged 2 commits into from
Sep 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions src/main/java/duke/tasks/TaskList.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package duke.tasks;

import java.util.ArrayList;
import java.util.function.IntPredicate;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import duke.exceptions.DukeInvalidArgumentException;
import duke.ui.CliUi;
Expand Down Expand Up @@ -180,13 +183,14 @@ public int size() {
* query.
*/
public ArrayList<Integer> findTasks(String query) {
ArrayList<Integer> result = new ArrayList<>();
String loweredQuery = query.toLowerCase();
for (int i = 0; i < this.tasks.size(); i++) {
if (this.tasks.get(i).getDescription().toLowerCase().contains(loweredQuery)) {
result.add(i);
}
}
return result;

IntPredicate indexContainsLoweredQuery = i -> this.tasks.get(i)
.getDescription().toLowerCase().contains(loweredQuery);

return IntStream.range(0, this.tasks.size())
.filter(indexContainsLoweredQuery)
.boxed()
.collect(Collectors.toCollection(ArrayList::new));
}
}
Loading