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 #4

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
11 changes: 11 additions & 0 deletions Lec6.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
36 changes: 34 additions & 2 deletions src/se/edu/inclass/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,16 @@ public class Main {
public static void main(String[] args) {
DataManager dm = new DataManager("./data/data.txt");
ArrayList<Task> tasksData = dm.loadData();
System.out.println("All data:");
printData(tasksData);

//code to stage
System.out.println("Printing deadlines");
printDeadlines(tasksData);

printDataUsingStream(tasksData);
printDeadlines(tasksData);
printDeadlinesUsingStream(tasksData);
System.out.println(countDeadlinesUsingStream(tasksData));
System.out.println("Total number of deadlines: " + countDeadlines(tasksData));

}
Expand All @@ -32,17 +38,43 @@ private static int countDeadlines(ArrayList<Task> tasksData) {
return count;
}

private static int countDeadlinesUsingStream(ArrayList<Task> tasks) {
System.out.println("count using stream");
int count = (int) tasks.stream()
.filter(t -> t instanceof Deadline)
.count();
return count;

}

public static void printData(ArrayList<Task> tasksData) {
System.out.println("Printing data using loop");
for (Task t : tasksData) {
System.out.println(t);
}
}

public static void printDataUsingStream(ArrayList<Task> tasks) {
System.out.println("Printing data using streams");
tasks.stream().
forEach(System.out::println);

}


public static void printDeadlines(ArrayList<Task> tasksData) {
System.out.println("Print deadlines using loop");
for (Task t : tasksData) {
if (t instanceof Deadline) {
System.out.println(t);
}
}
}
}

public static void printDeadlinesUsingStream(ArrayList<Task> tasks) {
System.out.println("Print deadlines using stream");
tasks.parallelStream()
.filter(t -> t instanceof Deadline) //lambda
.forEach(System.out::println);
}
}