1
+ package com .diyishuai .java8 .function ;
2
+
3
+ import lombok .AllArgsConstructor ;
4
+ import lombok .Data ;
5
+ import lombok .NoArgsConstructor ;
6
+
7
+ import java .util .ArrayList ;
8
+ import java .util .List ;
9
+ import java .util .function .BiFunction ;
10
+ import java .util .function .Function ;
11
+ import java .util .function .Supplier ;
12
+
13
+ /**
14
+ * @author Shea
15
+ * @date 2021-01-21
16
+ * @description
17
+ */
18
+ public class FunctionReference {
19
+
20
+ public static void main (String [] args ) {
21
+ classConstructorFunctionReference ();
22
+ classStaticFunctionReference ();
23
+ instanceFunctionReference ();
24
+ }
25
+
26
+
27
+ /**
28
+ * 类构造方法引用
29
+ */
30
+ private static void classConstructorFunctionReference () {
31
+ System .out .println ("类构造方法引用:" );
32
+ //构造引用
33
+ // 0 无参构造
34
+ Supplier <Apple > appleSupplier = Apple ::new ;
35
+ Apple a0 = appleSupplier .get ();
36
+ System .out .println (a0 );
37
+ // 1 单参构造
38
+ Function <String , Apple > appleFunction = Apple ::new ;
39
+ Apple a1 = appleFunction .apply ("1" );
40
+ System .out .println (a1 );
41
+ // 2 双参构造
42
+ BiFunction <String , Double , Apple > appleBiFunction = Apple ::new ;
43
+ Apple a2 = appleBiFunction .apply ("2" , 0.5 );
44
+ System .out .println (a2 );
45
+ }
46
+
47
+ /**
48
+ * 类静态方法引用
49
+ */
50
+ private static void classStaticFunctionReference () {
51
+ System .out .println ("类静态方法引用" );
52
+ List list = new ArrayList ();
53
+
54
+ list .add (new Apple ("1" ));
55
+ list .add (new Apple ("2" ));
56
+ list .add (new Apple ("3" ));
57
+ list .add (new Apple []{new Apple ("4" ), new Apple ("5" )});
58
+
59
+ list .stream ().forEach (Show ::show );
60
+
61
+ }
62
+
63
+
64
+ /**
65
+ * 实例方法引用
66
+ */
67
+ private static void instanceFunctionReference () {
68
+ System .out .println ("实例方法引用" );
69
+ List <Apple > list = new ArrayList ();
70
+
71
+ list .add (new Apple ("1" ));
72
+ list .add (new Apple ("2" ));
73
+ list .add (new Apple ("3" ));
74
+
75
+ list .stream ().map (a -> a .getId ()).forEach (System .out ::println );
76
+ System .out .println ("----" );
77
+ list .stream ().map (Apple ::getId ).forEach (System .out ::println );
78
+ }
79
+
80
+
81
+ }
82
+
83
+ @ Data
84
+ @ AllArgsConstructor
85
+ @ NoArgsConstructor
86
+ class Apple {
87
+
88
+ private String id ;
89
+ private double weight ;
90
+ private String color ;
91
+
92
+ public Apple (String id ) {
93
+ this .id = id ;
94
+ }
95
+
96
+ public Apple (String id , double weight ) {
97
+ this .id = id ;
98
+ this .weight = weight ;
99
+ }
100
+
101
+ public void show (){
102
+ System .out .println (this .toString ());
103
+ }
104
+
105
+ }
106
+
107
+ class Show {
108
+
109
+ public static void show (Object obj ) {
110
+ if (obj instanceof List ) {
111
+ for (Object o : (List ) obj ) {
112
+ System .out .print (o .toString () + "\t " );
113
+ }
114
+ System .out .println ();
115
+ } else if (obj instanceof Object []) {
116
+ for (Object o : (Object []) obj ) {
117
+ System .out .print (o .toString () + "\t " );
118
+ }
119
+ System .out .println ();
120
+ } else {
121
+ System .out .println (obj .toString ());
122
+ }
123
+ }
124
+
125
+ }
0 commit comments