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

Streams #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
23 changes: 20 additions & 3 deletions src/main/java/se/edu/streamdemo/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ public static void main(String[] args) {
ArrayList<Task> tasksData = dataManager.loadData();

System.out.println("Printing all data ...");
printAllData(tasksData);
printAllDataUsingStreams(tasksData);

System.out.println("Printing deadlines ...");
printDeadlines(tasksData);
printAllDeadlineUsingStreams(tasksData);

System.out.println("Total number of deadlines: " + countDeadlines(tasksData));
System.out.println("Total number of deadlines: " + coundDeadlineUsingStreams(tasksData));

}

Expand All @@ -38,6 +38,23 @@ public static void printAllData(ArrayList<Task> tasksData) {
}
}

/** The following method implements the previous one using streams instead of a for each loop
*
* @param tasks
*/
public static void printAllDataUsingStreams(ArrayList<Task> tasks) {
tasks.stream().forEach(System.out::println);
}
public static void printAllDeadlineUsingStreams(ArrayList<Task> tasks) {
tasks.stream().filter((t) -> t instanceof Deadline).forEach(System.out::println);
}
private static int coundDeadlineUsingStreams(ArrayList<Task> tasks) {
int count = 0;
count = (int) tasks.stream()
.filter((t) -> t instanceof Deadline).count();
return count;
}

public static void printDeadlines(ArrayList<Task> tasksData) {
for (Task t : tasksData) {
if (t instanceof Deadline) {
Expand Down