函数式编程
小于 1 分钟
分类
supplier
没有参数,提供一个结果
()->{return ...}函数
一个或多个参数,一个结果
(参数)->{return ...}consumer
一个或多个参数,没有结果
(参数)->{...}使用例子
//这里有类型自动推导
public <T> void demo(
Supplier<T> supplire,
Function<T,Integer> function,
//代表有两个参数,无返回值
BiConsumer<T,Integer> biconsumer,
Consumer<T> consumer
){
T nums=supplire.get();
Integer in=function.apply(T);
consumer.accept(T);
}
demo(
//提供数组
()->{return new int[10];},
//获取数组长度
(name)->{return name.length();},
//打印数组10次
(name,10)->{System.out.print("...")},
//打印数组
(name)->{System.out.print(name)}
)