forked from Apress/functional-interfaces-in-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSect9a_Ex2.java
More file actions
37 lines (33 loc) · 1.09 KB
/
Sect9a_Ex2.java
File metadata and controls
37 lines (33 loc) · 1.09 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
package chapter12;
import java.util.function.*;
import java.util.*;
import java.util.stream.*;
public class Sect9a_Ex2
{
public static void main(String[] args)
{
Supplier<List<Character>> supp = () -> new ArrayList<Character>();
BinaryOperator<List<Character>> comb1 = (x,y) -> {
x.addAll(y);
return x;
};
BiConsumer<List<Character>,Character> accc2 = (x,y) -> {
if (Character.isAlphabetic(y))
x.add(0,y);
else
x.add(y);
};
Function<List<Character>,String> fins2 = x -> {
String t="";
for (Character c : x)
t += c;
return t;
};
String t =
Stream.of('1','a','b','2') // Stream<Character>
.collect(Collectors.collectingAndThen(
Collector.of(supp, accc2, comb1), // List<Character>
fins2)); // String
System.out.println(t); // Prints ba12
}
}