|
3 | 3 | import java.io.Serializable; |
4 | 4 | import java.util.Arrays; |
5 | 5 | import java.util.Collection; |
| 6 | +import java.util.Optional; |
6 | 7 |
|
7 | | -public class LambdaDemo<T extends Serializable&Comparable&Collection> { |
8 | | - |
9 | | - public static void main(String args[]){ |
| 8 | +public class LambdaDemo<T extends Serializable & Comparable & Collection> { |
| 9 | + |
| 10 | + public static void main(String args[]) { |
10 | 11 | LambdaDemo demo = new LambdaDemo(); |
11 | | - |
| 12 | + |
12 | 13 | MathOperation op = new MathOperation<Integer>() { |
13 | 14 | @Override |
14 | 15 | public Integer operation(int a, int b) { |
15 | 16 | return 1; |
16 | 17 | } |
17 | 18 | }; |
18 | | - |
| 19 | + |
19 | 20 | MathOperation op1 = (a, b) -> 1; |
20 | | - |
| 21 | + |
21 | 22 | // 类型声明 |
22 | 23 | MathOperation addition = (int a, int b) -> a + b; |
23 | | - |
24 | | - // 不用类型声明 |
| 24 | + |
| 25 | + // 不用类型声明 |
25 | 26 | MathOperation subtraction = (a, b) -> a - b + 1.0; |
26 | | - |
| 27 | + |
27 | 28 | // 大括号中的返回语句 |
28 | | - MathOperation multiplication = (int a, int b) -> { |
| 29 | + MathOperation multiplication = (int a, int b) -> { |
29 | 30 | int c = 1000; |
30 | | - return a * b + c; |
| 31 | + return a * b + c; |
31 | 32 | }; |
32 | | - |
| 33 | + |
33 | 34 | // 没有大括号及返回语句 |
34 | 35 | MathOperation division = (int a, int b) -> a / b; |
35 | | - |
| 36 | + |
36 | 37 | System.out.println("10 + 5 = " + demo.operate(10, 5, addition)); |
37 | 38 | System.out.println("10 - 5 = " + demo.operate(10, 5, subtraction)); |
38 | 39 | System.out.println("10 x 5 = " + demo.operate(10, 5, multiplication)); |
39 | 40 | System.out.println("10 / 5 = " + demo.operate(10, 5, division)); |
40 | | - |
| 41 | + |
41 | 42 | //System.out.println("10 ^ 5 = " + demo.operate(10, 5, (a, b) -> new Double(Math.pow(a,b)).intValue())); |
42 | | - |
43 | | - System.out.println("10 ^ 5 = " + demo.operate(10, 5, (a, b) -> Math.pow(a,b))); |
44 | | - |
| 43 | + |
| 44 | + System.out.println("10 ^ 5 = " + demo.operate(10, 5, (a, b) -> Math.pow(a, b))); |
| 45 | + |
45 | 46 | // 不用括号 |
46 | 47 | GreetingService greetService1 = message -> |
47 | 48 | System.out.println("Hello " + message); |
48 | | - |
| 49 | + |
49 | 50 | // 用括号 |
50 | 51 | GreetingService greetService2 = (message) -> |
51 | 52 | System.out.println("Hello " + message); |
52 | | - |
| 53 | + |
53 | 54 | GreetingService greetService3 = System.out::println; |
54 | | - |
55 | | - Arrays.asList(1,2,3).forEach( x -> System.out.println(x+3)); |
56 | | - Arrays.asList(1,2,3).forEach( LambdaDemo::println ); |
57 | | - |
| 55 | + |
| 56 | + Arrays.asList(1, 2, 3).forEach(x -> System.out.println(x + 3)); |
| 57 | + Arrays.asList(1, 2, 3).forEach(LambdaDemo::println); |
| 58 | + |
58 | 59 | greetService1.sayMessage("kimmking"); |
59 | 60 | greetService2.sayMessage("Java"); |
60 | 61 | } |
61 | | - |
| 62 | + |
62 | 63 | private static void println(int x) { |
63 | | - System.out.println(x+3); |
| 64 | + System.out.println(x + 3); |
64 | 65 | } |
65 | | - |
| 66 | + |
66 | 67 | interface MathOperation<T> { |
67 | 68 | T operation(int a, int b); // 返回类型+函数名+参数类型的列表 |
68 | 69 | } |
69 | | - |
| 70 | + |
70 | 71 | interface GreetingService { |
71 | 72 | void sayMessage(String message); |
72 | 73 | } |
73 | | - |
74 | | - private <T> T operate(int a, int b, MathOperation<T> mathOperation){ |
| 74 | + |
| 75 | + private T operate(int a, int b, MathOperation<T> mathOperation) { |
75 | 76 | return mathOperation.operation(a, b); |
76 | 77 | } |
77 | | - |
| 78 | + |
78 | 79 | } |
0 commit comments