四大函数式接口
代码:
/**
* 函数式接口测试
*
* @author wangmaoyu
* @create 2018-03-09 上午12:44
**/
public class TestFunction {
/**
* 消费型接口,接受一个参数无返回值
*/
public static void testCommon1(){
Consumer<String> consumer=(str)-> System.out.println(str);
consumer.accept("我是消费型接口");
}
/**
* 供给型接口无参数有返回值
*/
public static void testCommon2(){
Supplier<Date> supplier=()-> new Date();
Date date=supplier.get();
System.out.println("当前时间:"+date);
}
/**
* 函数式接口,接受一个参数,有返回值
*/
public static void testCommon3(){
Function<String,String> fun=(str)-> "hello,"+str;
String str = fun.apply("李元芳");
System.out.println(str);
}
/**
* 断言式接口,接受一个参数,返回值Boolean值
*/
public static void testCommon4(){
Predicate<Integer> predicate=(num)->num>0;
boolean test = predicate.test(10);
System.out.println(test);
}
public static void main(String[] args) {
TestFunction.testCommon1();
TestFunction.testCommon2();
TestFunction.testCommon3();
TestFunction.testCommon4();
}
}
效果: