-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAndThenDemo.java
More file actions
29 lines (26 loc) · 1.09 KB
/
Copy pathAndThenDemo.java
File metadata and controls
29 lines (26 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
package lambda;
import java.util.function.Consumer;
/**
* @author : CodeWater
* @create :2022-03-01-15:58
* @Function Description :andThen接口练习
* 按照格式“ 姓名:XX。性别:XX。 ”的格式将信息打印出来。要求将打印姓
* 名的动作作为第一个 Consumer 接口的Lambda实例,将打印性别的动作作为第二个 Consumer 接口的Lambda实
* 例,将两个 Consumer 接口按照顺序“拼接”到一起。
* String[] array = { "迪丽热巴,女", "古力娜扎,女", "马尔扎哈,男" };
*/
public class AndThenDemo {
public static void main(String[] args) {
String[] array = {"迪丽热巴,女", "古力娜扎,女", "马尔扎哈,男"};
printInfo(
s -> System.out.print("姓名:" + s.split(",")[0]),
s -> System.out.println(",性别:" + s.split(",")[1] + "。"),
array
);
}
private static void printInfo(Consumer<String> one, Consumer<String> two, String[] array) {
for (String info : array) {
one.andThen(two).accept(info);
}
}
}