java常用函数式接口

四大函数式接口

代码:

/**
 * 函数式接口测试
 *
 * @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();

    }
}

效果:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值