-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsection.java.util.function.xml
More file actions
630 lines (530 loc) · 16.8 KB
/
section.java.util.function.xml
File metadata and controls
630 lines (530 loc) · 16.8 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
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
<?xml version="1.0" encoding="UTF-8"?>
<chapter id="index"><?dbhtml dir="function" ?>
<!-- java.util.function -->
<title>函数式编程</title>
<section id="Supplier">
<title>Supplier 供应型的接口</title>
<programlisting>
<![CDATA[
Supplier<String> supplier = () -> "hello, world";
String result = supplier.get();
System.out.println(result);
Supplier stringSupplier = () -> new String("Hi Neo");
String string = stringSupplier.get();
System.out.println(string);
Supplier<LocalDateTime> currentTime = () -> LocalDateTime.now();
LocalDateTime now = currentTime.get(); // 计算当前时间
Optional<String> optional = Optional.ofNullable("hello");
String orElseGet = optional.orElseGet(() -> "world");
Supplier userSupplier= () -> new User(1,"netkiller");
User user=userSupplier.get();
System.out.println(user.getName());
]]>
</programlisting>
<programlisting>
<![CDATA[
package cn.netkiller.test;
import java.util.function.Supplier;
public class TestSupplier {
private final int age = 24;
TestSupplier() {
System.out.println(age);
}
public static void main(String[] args) {
Supplier<TestSupplier> supplier = TestSupplier::new;
//调用get()方法,此时会调用对象的构造方法,即获得到真正对象
supplier.get();
System.out.println(supplier.get().test());
TestSupplier test = supplier.get();
System.out.println(test.test());
}
private String test() {
return "Helloworld!!!";
}
}
]]>
</programlisting>
<programlisting>
<![CDATA[
package cn.netkiller.test;
import java.util.function.Supplier;
public class TestSupplier {
private final int age = 24;
private final String name;
TestSupplier(String name) {
this.name = name;
// System.out.println(name);
}
public static void main(String[] args) {
Supplier<String> stringSupplier = () -> {
return "test1";
};
System.out.println(stringSupplier.get());
Supplier<String> stringSupplier1 = () -> "test2";
System.out.println(stringSupplier1.get());
Supplier<TestSupplier> testSupplier = () -> new TestSupplier("Neo");
System.out.println(testSupplier.get().name);
// String name = "Netkiller";
// Supplier<TestSupplier> testSupplier1 = (name) -> {new TestSupplier(name)};
// System.out.println(testSupplier1.get().name);
// System.out.println(test.test());
}
private String test() {
return "Helloworld!!!";
}
}
]]>
</programlisting>
<section>
<title>Supplier 作为方法参数使用</title>
<programlisting>
<![CDATA[
package cn.netkiller.test;
import java.util.concurrent.ExecutionException;
import java.util.function.Supplier;
public class Test {
public static void main(String[] args) throws ExecutionException, InterruptedException {
System.out.println(Thread.currentThread());
process(() -> {
System.out.println(Thread.currentThread().getName() + " Process...");
return "Test";
});
Supplier<String> stringSupplier1 = () -> "netkiller";
process(stringSupplier1);
}
public static <T> void process(Supplier<T> supplier) {
// System.out.println(Thread.currentThread().getName() + " Process...");
// CompletableFuture<T> runAsync = CompletableFuture.supplyAsync(supplier);
// runAsync.join();
// System.out.println(variable.get());
T value = supplier.get();
System.out.println(value);
}
}
]]>
</programlisting>
</section>
<section id="IntSupplier">
<title>IntSupplier</title>
<para>IntSupplier - 有getAsInt()方法</para>
<programlisting>
<![CDATA[
package cn.netkiller.test;
import java.util.function.IntSupplier;
import java.util.stream.IntStream;
public class Test {
public static void main(String[] args) {
IntSupplier naturalGenerator = new IntSupplier() {
int currentValue = 0;
public int getAsInt() {
return currentValue++;
}
};
IntStream.range(0, 10).forEach((n) -> {
System.out.println(naturalGenerator.getAsInt());
});
}
}
]]>
</programlisting>
<para>自定义 getAsInt 抽象方法,可以定制步进值</para>
<programlisting>
<![CDATA[
package cn.netkiller.test;
import java.util.function.IntSupplier;
import java.util.stream.IntStream;
public class Test {
public static void main(String[] args) {
System.out.println(Thread.currentThread());
IntSupplier naturalGenerator = new IntSupplier() {
int currentValue = 1;
public int getAsInt() {
return currentValue *= 2;
}
};
IntStream.range(0, 10).forEach((n) -> {
System.out.println(naturalGenerator.getAsInt());
});
}
}
]]>
</programlisting>
</section>
<section id="LongSupplier">
<title>LongSupplier</title>
<para>LongSupplier - 有getAsLong()方法</para>
<programlisting>
<![CDATA[
LongSupplier longSupplier1 = () -> LocalDate.now().toEpochDay();
System.out.println(longSupplier1.getAsLong());
LongSupplier longSupplier2 = () -> Long.parseLong("1024");
System.out.println(longSupplier2.getAsLong());
LongSupplier longSupplier3 = () -> {
long num1 = 30L;
long num2 = 50L;
return num1 * num2;
};
System.out.println(longSupplier3.getAsLong());
LongSupplier time = new Date()::getTime;
System.out.println(time.getAsLong());
]]>
</programlisting>
<programlisting>
<![CDATA[
public static void main(String[] args) {
String id = test(() -> new Random().nextLong());
System.out.println(id);
}
public static String test(LongSupplier ls) {
return "no. " + ls.getAsLong();
}
]]>
</programlisting>
</section>
<section id="DoubleSupplier">
<title>DoubleSupplier</title>
<para>DoubleSupplier - 有getAsDouble()方法</para>
<programlisting>
<![CDATA[
DoubleSupplier random = () -> new Random().nextDouble();
System.out.println(random.getAsDouble());
DoubleSupplier doubleSupplier = () -> Double.parseDouble("123.0987");
System.out.println(doubleSupplier.getAsDouble());
DoubleSupplier doubleSupplier1 = () -> {
double val1 = 35.30;
double val2 = 45.97;
return val1 * val2;
};
System.out.println(doubleSupplier1.getAsDouble());
DoubleSupplier random1 = new Random()::nextDouble;
System.out.println(random1.getAsDouble());
]]>
</programlisting>
</section>
<section id="BooleanSupplier">
<title>BooleanSupplier</title>
<para>BooleanSupplier - 有getAsBoolean()方法。</para>
<programlisting>
<![CDATA[
BooleanSupplier booleanSupplier1 = () -> LocalDate.now().isLeapYear();
System.out.println(booleanSupplier1.getAsBoolean());
BooleanSupplier booleanSupplier2 = () -> "netkiller".length() > 5;
System.out.println(booleanSupplier2.getAsBoolean());
BooleanSupplier booleanSupplier3 = () -> {
int num = 16;
if (num % 2 == 0) {
return true;
}
return false;
};
System.out.println(booleanSupplier3.getAsBoolean());
]]>
</programlisting>
</section>
</section>
<section id="Consumer">
<title>Consumer 消费型的接口</title>
<programlisting>
<![CDATA[
Consumer<String> printer = (s) -> System.out.println(s);
// 使用 accept 方法执行操作
printer.accept("Hello, World!");
]]>
</programlisting>
<programlisting>
<![CDATA[
Consumer<String> upperCasePrinter = (s) -> System.out.println(s.toUpperCase());
Consumer<String> lowerCasePrinter = (s) -> System.out.println(s.toLowerCase());
// 使用 andThen 方法连接两个 Consumer
Consumer<String> combinedPrinter = upperCasePrinter.andThen(lowerCasePrinter);
combinedPrinter.accept("Hello world!!!");
]]>
</programlisting>
<programlisting>
<![CDATA[
package cn.netkiller;
import java.util.function.Consumer;
import lombok.Data;
/**
* TestConsumer
*/
@Data
public class TestConsumer {
private String name;
private String nickname;
private int age;
public void init(Consumer<TestConsumer> configurator) {
configurator.accept(this);
}
public static void main(String[] args) {
System.out.println("Hello world!");
TestConsumer test = new TestConsumer();
// 使用 Consumer 配置 Person 对象
test.init(p -> {
p.setName("Neo");
p.setNickname("netkiller");
p.setAge(30);
});
System.out.println(test);
}
}
]]>
</programlisting>
<para>andThen</para>
<programlisting>
<![CDATA[
Consumer<Integer> test = (n) -> {
System.out.println(n);
};
test.andThen((v) -> {
System.out.println(v);
}).accept(100);
]]>
</programlisting>
<section id="IntConsumer">
<title>IntConsumer</title>
<programlisting>
<![CDATA[
IntConsumer printNumber = num -> System.out.println("The number is: " + num);
printNumber.accept(42); // 输出: The number is: 42
]]>
</programlisting>
</section>
<section id="Consumer.Void">
<title>无返回值 Void</title>
<section>
<title>定义 Consumer<Void></title>
<programlisting>
<![CDATA[
public void play(String url, ProgressBar progressBar, Consumer<Void> consumer) {
if (neoPlayer == null) {
neoPlayer = NeoPlayer.getInstance();
Log.i(TAG, "MediaPlayer 创建实例");
}
if (url == null || url.isEmpty()) {
Log.i(TAG, "Play error: " + url);
return;
} else {
Log.i(TAG, "Play: " + url);
}
try {
neoPlayer.resetPlayer();
neoPlayer.setDataSource(url);//设置播放来源
neoPlayer.prepareAsync();//异步准备
} catch (IOException e) {
Log.d(TAG, e.toString());
} catch (IllegalStateException e) {
Log.d(TAG, e.toString());
}
neoPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
//异步准备监听
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
// Log.i(TAG, "Voice 播放异步文件,时长: " + mediaPlayer.getDuration() / 1000 + " Audio: " + url);
mediaPlayer.start();//播放
if (progressBar != null) {
progress(progressBar);
progressBar.setVisibility(View.VISIBLE);
}
}
});
neoPlayer.setScreenOnWhilePlaying(true);// 设置播放的时候一直让屏幕变亮
neoPlayer.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {
//文件缓冲监听
@Override
public void onBufferingUpdate(MediaPlayer mediaPlayer, int i) {
Log.i(TAG, "onBufferingUpdate Voice 缓冲区加载进度: " + i + "%");
}
});
neoPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
progressBar.setVisibility(View.GONE);
consumer.accept(null);
}
});
}
]]>
</programlisting>
</section>
<section>
<title>回调写法</title>
<programlisting>
<![CDATA[
aigcSpeech.Player().play(Config.Cloud.resUrl.concat(chat.audio), holder.progressBarAudio, (Void) -> {
holder.toggleButtonPlayer.setChecked(false);
});
]]>
</programlisting>
</section>
</section>
</section>
<section id="BiConsumer">
<title>BiConsumer</title>
<para>BiConsumer 可以返回两个参数</para>
<programlisting>
<![CDATA[
BiConsumer<String, Integer> printInfo = (name, age) -> System.out.println(name + " is " + age + " years old.");
printInfo.accept("Neo", 25); // 输出: Neo is 25 years old.
]]>
</programlisting>
</section>
<section id="BiFunction">
<title>BiFunction</title>
<programlisting>
<![CDATA[
package cn.netkiller.test;
import java.util.concurrent.ExecutionException;
import java.util.function.BiFunction;
public class Test {
public static void main(String[] args) {
BiFunction<String, String, Integer> biFunction = (s1, s2) -> s1.length() + s2.length();
Integer length = biFunction.apply("Neo", "netkiller");
System.out.println(length);
}
}
]]>
</programlisting>
<para></para>
<programlisting>
<![CDATA[
package cn.netkiller.test;
import java.util.concurrent.ExecutionException;
import java.util.function.BiFunction;
public class Test {
public static void main(String[] args) {
BiFunction<String, String, Integer> biFunction = (s1, s2) -> s1.length() + s2.length();
Integer length = biFunction.andThen(len -> len * 2).apply("Neo", "netkiller");
System.out.println(length);
}
}
]]>
</programlisting>
</section>
<section id="Predicate">
<title>Predicate 判断型的接口</title>
<programlisting>
<![CDATA[
Predicate<Integer> predicate = i -> i > 5;
System.out.println(predicate.test(1));
System.out.println(predicate.test(10));
]]>
</programlisting>
</section>
<section id="java.util.function">
<title>Supplier / Consumer / Predicate 应用场景</title>
<para>Supplier 没有入参,但是有返回值,通过 get() 获得。</para>
<para>Consumer 有入参,但是没有返回值,通过 andThen() 可以获得返回叔叔。</para>
<programlisting>
<![CDATA[
package cn.netkiller.test;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.function.Supplier;
public class Test {
public static void main(String[] args) {
System.out.println(Thread.currentThread());
Supplier<String> supplier = () -> "hello, world";
String result = supplier.get();
System.out.println("Supplier: " + result);
Consumer<String> consumer = (s) -> System.out.println(s);
// consumer.accept("Hello, Neo!");
Consumer<String> then = consumer.andThen(value -> {
System.out.println("Consumer: " + value);
});
consumer.accept("Hello, Neo!");
then.accept("Hello, Netkiller!");
Predicate<Integer> predicate = i -> i > 5;
System.out.println(predicate.test(1));
System.out.println(predicate.test(10));
Predicate<String> string = s -> !s.isEmpty();
string.or(s -> s.equals("neo"));
System.out.println(string.test(""));
System.out.println(string.test("neo"));
}
}
]]>
</programlisting>
</section>
<section>
<title>Map + Function</title>
<programlisting>
<![CDATA[
package cn.netkiller.test;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
public class Test {
static void main(String[] args) {
Map<String, Function<String, String>> map = new HashMap<>();
map.put("A", s -> "处理逻辑 A: " + s);
map.put("B", s -> "处理逻辑 B: " + s);
map.put("C", s -> "处理逻辑 C: " + s);
String input = "B";
String result = map.getOrDefault(input, s -> "默认逻辑").apply(input);
System.out.println(result);
}
}
]]>
</programlisting>
</section>
<section>
<title>Predicate + Stream</title>
<programlisting>
<![CDATA[
package cn.netkiller.test;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
class Rule {
Predicate<Integer> condition;
Runnable action;
Rule(Predicate<Integer> condition, Runnable action) {
this.condition = condition;
this.action = action;
}
}
public class Test {
static void main(String[] args) {
int value = 15;
List<Rule> rules = Arrays.asList(
new Rule(v -> v < 10, () -> System.out.println("小于 10")),
new Rule(v -> v >= 10 && v < 20, () -> System.out.println("10 到 20")),
new Rule(v -> v >= 20, () -> System.out.println("大于等于 20"))
);
rules.stream()
.filter(rule -> rule.condition.test(value))
.findFirst()
.ifPresent(rule -> rule.action.run());
}
}
]]>
</programlisting>
</section>
<section>
<title>Enum + Function</title>
<programlisting>
<![CDATA[
package cn.netkiller.test;
import java.util.function.Function;
enum Operation {
ADD(x -> x + 10), MULTIPLY(x -> x * 2), SQUARE(x -> x * x);
private final Function<Integer, Integer> func;
Operation(Function<Integer, Integer> func) {
this.func = func;
}
public int apply(int value) {
return func.apply(value);
}
}
public class Test {
static void main(String[] args) {
// 输出 10
int result = Operation.MULTIPLY.apply(5);
System.out.println(result);
}
}
]]>
</programlisting>
</section>
</chapter>