forked from vpm238/SF-IntJava-Apr
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExamples.java
More file actions
47 lines (39 loc) · 1.7 KB
/
Examples.java
File metadata and controls
47 lines (39 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package lambdaapis;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public final class Examples {
public static void main(String[] args) {
List<String> words = new ArrayList<>(List.of("hello", "goodbye", "short", "reallyverylong"));
words.forEach(s -> System.out.println(s));
System.out.println("------------------------");
words.replaceAll(s -> s.toUpperCase());
words.forEach(s -> System.out.println(s));
System.out.println("------------------------");
words.removeIf(s -> s.length() < 6);
words.forEach(s -> System.out.println(s));
System.out.println("------------------------");
LocalDate today = LocalDate.now();
LocalDate tomorrow = today.plusDays(1);
LocalDate yesterday = today.minusDays(1);
Map<LocalDate, String> workCal = new HashMap<>(Map.of(
today, "Clear desk",
tomorrow, "Leave early"
));
Map<LocalDate, String> homeCal = Map.of(
yesterday, "Buy party food",
tomorrow, "Party all night long!"
);
workCal.forEach((k,v) -> System.out.printf("On %1$tm/%0$te %1s\n", k, v ));
System.out.println("----------------------------");
workCal.compute(today, (ld, s) -> s + " carefully so boss doesn't complain!");
workCal.forEach((k,v) -> System.out.printf("On %1$tm/%0$te %1s\n", k, v ));
System.out.println("----------------------------");
Map<LocalDate, String> combinedCal = new HashMap<>();
workCal.forEach((k,v) -> combinedCal.put(k, v));
homeCal.forEach((k,v) -> combinedCal.merge(k, v, (k1, v1) -> v + " and " + v1));
combinedCal.forEach((k,v) -> System.out.printf("On %1$tm/%0$te %1s\n", k, v ));
}
}