-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLambdaExamples.java
More file actions
42 lines (32 loc) · 1.13 KB
/
Copy pathLambdaExamples.java
File metadata and controls
42 lines (32 loc) · 1.13 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
package Session_17_lambda;
public class LambdaExamples {
public static void main(String... args) {
Printable printable = s -> {
System.out.println("salut");
return "ciau" + s;
};
printThing(printable);
// printThing(() -> System.out.println("salut"));
StringProcessor removeWhitespaces = str -> str.replace(" ", "");
StringProcessor toUpperCase = str -> str.toUpperCase();
StringProcessor combined = removeWhitespaces.andThen(toUpperCase);
System.out.println(combined.process("hello world"));
Calculator calculator = (a, b, operator) -> calcImpl(a,b,operator);
System.out.println(calculator.calculate(3, 5, '+'));
}
public static double calcImpl(double a, double b, char op){
switch (op) {
case '+':
return a + b;
case '-':
return a - b;
case '*':
return a * b;
default:
return a / b;
}
}
public static void printThing(Printable print) {
System.out.println(print.print("!"));
}
}