forked from Apress/functional-interfaces-in-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSect9_Ex1.java
More file actions
29 lines (27 loc) · 930 Bytes
/
Sect9_Ex1.java
File metadata and controls
29 lines (27 loc) · 930 Bytes
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
package chapter12;
import java.util.function.*;
import java.util.*;
import java.util.stream.*;
public class Sect9_Ex1
{
public static void main(String[] args)
{
Supplier<List<Character>> supp = () -> new ArrayList<Character>();
BiConsumer<List<Character>, Character> acc = (x,y) -> {
System.out.print("acc: x=" + x + " y=" + y+ " result=");
if (Character.isAlphabetic(y))
x.add(0,y);
else
x.add(y);
x.forEach(z -> System.out.print(z));
System.out.println();
};
BinaryOperator<List<Character>> comb1 = (x,y) -> {
x.addAll(y);
return x;
};
Stream.of('1','a','b','2') // Stream<Character>
.collect(Collector.of(supp, acc, comb1)) // List<Character>
.forEach(x -> System.out.println(x));
}
}