Skip to content

Commit 57551c7

Browse files
asifiqbal31shekhargulati
authored andcommitted
Just filling in some potentially missing code (#19)
* Adding call to getTitle() inside TaskExtractor interface * Filling in potentially missing piece of code * Reverting one of the previous changes, was a mistake on my part
1 parent 1808327 commit 57551c7

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

02-lambdas.md

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -492,14 +492,27 @@ public class Exercise_Lambdas {
492492
Using method reference for extractor
493493

494494
```java
495-
public static void main(String[] args) {
496-
List<Task> tasks = getTasks();
497-
List<String> titles = filterAndExtract(tasks, task -> task.getType() == TaskType.READING, Task::getTitle);
498-
titles.forEach(System.out::println);
499-
List<LocalDate> createdOnDates = filterAndExtract(tasks, task -> task.getType() == TaskType.READING, Task::getCreatedOn);
500-
createdOnDates.forEach(System.out::println);
501-
List<Task> filteredTasks = filterAndExtract(tasks, task -> task.getType() == TaskType.READING, Function.identity());
502-
filteredTasks.forEach(System.out::println);
495+
public class Exercise_Lambdas {
496+
497+
public static void main(String[] args) {
498+
List<Task> tasks = getTasks();
499+
List<String> titles = filterAndExtract(tasks, task -> task.getType() == TaskType.READING, Task::getTitle);
500+
titles.forEach(System.out::println);
501+
List<LocalDate> createdOnDates = filterAndExtract(tasks, task -> task.getType() == TaskType.READING, Task::getCreatedOn);
502+
createdOnDates.forEach(System.out::println);
503+
List<Task> filteredTasks = filterAndExtract(tasks, task -> task.getType() == TaskType.READING, Function.identity());
504+
filteredTasks.forEach(System.out::println);
505+
}
506+
507+
public static <R> List<R> filterAndExtract(List<Task> tasks, Predicate<Task> filterTasks, Function<Task, R> extractor) {
508+
List<R> readingTitles = new ArrayList<>();
509+
for (Task task : tasks) {
510+
if (filterTasks.test(task)) {
511+
readingTitles.add(extractor.apply(task));
512+
}
513+
}
514+
return readingTitles;
515+
}
503516
}
504517
```
505518

0 commit comments

Comments
 (0)