关于Java泛型和Lambda表达式的一些例子

代码:

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import java.util.function.Function;

public class Main6 {

public static void main(String[] args) {
boolean debug = true;

if (debug) {
TestM.myrun();
}

if (debug == false) {

MyBox<String> box = new MyBox<>("hello");

// 显式指定 map 的返回类型为 Integer
Integer length = box.<Integer>map(str -> str.length()).get();
System.out.println("长度是:" + length);

// 显式指定 map 的返回类型为 ResponseEntity<Void>
ResponseEntity<Void> resp = box.<ResponseEntity<Void>>map(str -> {
System.out.println("值是:" + str);
// return ResponseEntity.noContent().build();
return ResponseEntity.noContent();
}).orElse(ResponseEntity.<Void>notFound());

System.out.println(resp);
}
}

}


class MyBox<T> {
private final T value;

public MyBox(T value) {
this.value = value;
}

// 工厂方法:空盒子
public static <T> MyBox<T> empty() {
return new MyBox<>(null);
}

// 工厂方法:非空盒子
public static <T> MyBox<T> of(T value) {
return new MyBox<>(value);
}

// 是否有值
public boolean isPresent() {
return value != null;
}

public T get() {
return value;
}

// 泛型 map 方法
public <R> MyBox<R> map(Function<? super T, ? extends R> func) {
if (value != null) {
return new MyBox<>(func.apply(value));
} else {
return new MyBox<>(null);
}
}

// orElse:如果有值,返回它;否则返回默认值
public T orElse(T defaultValue) {
return value != null ? value : defaultValue;
}

public static void main(String[] args) {
MyBox<String> box = MyBox.of("hello");

// 模拟你写的结构:map + orElse
ResponseEntity<Void> result = box.<ResponseEntity<Void>>map(str -> {
System.out.println("处理内容:" + str);
return ResponseEntity.noContent();
}).orElse(ResponseEntity.<Void>notFound());

System.out.println(result);

// 测试空值情况
MyBox<String> emptyBox = MyBox.empty();
ResponseEntity<Void> result2 = emptyBox.<ResponseEntity<Void>>map(str -> ResponseEntity.noContent()).orElse(ResponseEntity.<Void>notFound());

System.out.println(result2);
}
}

// 模拟 ResponseEntity(简化版)
class ResponseEntity<T> {
private final int status;

private ResponseEntity(int status) {
this.status = status;
}

public static <T> ResponseEntity<T> noContent() {
return new ResponseEntity<>(204);
}

public static <T> ResponseEntity<T> notFound() {
return new ResponseEntity<>(404);
}

@Override
public String toString() {
return "ResponseEntity: " + status;
}
}


class TestM {
public static void myrun() {
// System.out.println(123);
//
// Test2.print2_1(new Test3<>("hello"));
// Test2.print2_2(new Test3<>(new Test3<>("hello2")));
//
// Test2.print2_3(new Test3<>(new Test3<>("hello3")));


// MyFun myfun = (in)->{
// return in;
// };

// Test2.<String>print2_myfun((in) -> {
// return in;
// });


Test4<Test3<String>> test4 = new Test4(new Test3<>("hi,Test3"));

System.out.println(test4.<Test3<String>>print2_myfun((in) -> {
return in;
}));

Test4<Test4<String>> test4_2 = new Test4(new Test4<>("hi,Test4"));

System.out.println(test4_2.<Test4<String>>print2_myfun((in) -> {
return in;
}).orElse("hi,I null."));


// Test4<Test4<String>> test4_3 = new Test4("hi,Test4_2");
// // 显式指定 map 的返回类型为 ResponseEntity<Void>
// ResponseEntity<Void> resp = test4_3.<ResponseEntity<Void>>map(str -> {
// System.out.println("值是:" + str);
//// return ResponseEntity.noContent().build();
// return ResponseEntity.noContent();
// }).orElse(ResponseEntity.<Void>notFound());
//
// System.out.println(resp);

Test4<String> test4_3 = new Test4("hi,Test4_3");

String out4_3 = test4_3.map((in)->{return in;}).orElse("hi,I null in Test4_3.");
System.out.println(out4_3);

// Test4<String> test4_4 = new Test4("hi,Test4_4");
//
// Object out4_4 = test4_4.orElse("hi,I null in Test4_4.").map((in)->{return in;});
// System.out.println(out4_4);




Test4<String> test4_5 = new Test4("hi,Test4_5");

String out4_5 = test4_5.map((in)->{return in;}).<Object>orElse("hi,I null in Test4_5.");
System.out.println(out4_5);



}


}

class Test2<T2> {
private T2 data;

public T2 getData() {
return data;
}

public void setData(T2 data) {
this.data = data;
}

public static <T2_1> void print2_1(Test3<? extends T2_1> data) {
System.out.println(data.getClass().getName() + " : " + data);

}

public static <T2_2> void print2_2(Test3<Test3<? extends T2_2>> data) {
System.out.println(data.getClass().getName() + " : " + data);

}

public static <T2_3> void print2_3(Test3<? extends Test3> data) {
System.out.println(data.getClass().getName() + " : " + data);

}

public static <T2_4, T2_4_2> void print2_4(Test3<? extends T2_4> data) {
System.out.println(data.getClass().getName() + " : " + data);

}

// public <T2_MF1_in> void print2_myfun(MyFun<T2_MF1_in> myFun) {
//
// System.out.println(myFun.mydo(""));
//
//
// }
}

class Test3<T3> {
public Test3(T3 data) {
this.data = data;
}

private T3 data;

public T3 getData() {
return data;
}

public void setData(T3 data) {
this.data = data;
}

@Override
public String toString() {
return "Test3{" + "data=" + data + '}';
}
}

class Test4<T4_in> {
public Test4(T4_in indata) {
data = indata;
}

public T4_in data;

public <T4_out> T4_out print2_myfun(MyFun<T4_in, T4_out> myFun) {
// System.out.println(myFun.mydo(data));

System.out.println("data type=" + data.getClass().getName());

return myFun.mydo(data);

}

// orElse:如果有值,返回它;否则返回默认值
public T4_in orElse(T4_in defaultValue) {
return data != null ? data : defaultValue;
}

// 泛型 map 方法
public <R> Test4<R> map(MyFun<? super T4_in, ? extends R> func) {
if (data != null) {
return new Test4<>(func.mydo(data));
} else {
return new Test4<>(null);
}
}

}

interface MyFun<TMF_in, TMF_out> {
TMF_out mydo(TMF_in in);
}