java function

简述

  • Function<A, B> : 一个参数 + 返回值
  • Supplier<A> : 无参数 + 返回值

示例

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
public class FunctionTest {

// Function<A, B> : 一个参数 + 返回值
// Supplier<A> : 无参数 + 返回值
static class FC implements Function<String, String> {
private final Function<String, String> f;
private final Supplier<Long> spl;
private final Function<String[], String> mf;

public FC(Function<String, String> f,
Supplier<Long> spl,
Function<String[], String> mf) {
this.f = f;
this.spl = spl;
this.mf = mf;
}

@Override
public String apply(String s) {
return spl.get() + " " + f.apply(s);
}

public String rmf(String[] ss) {
return mf.apply(ss);
}
}

public static void main(String[] args) {
FC fc = new FC((item) -> "Hello " + item,
System::currentTimeMillis,
(ags) -> String.join(",", ags));
System.out.println(fc.apply(fc.rmf(new String[]{"Tom", "Kitty"})));
}
}