lambda表达式——快速入门

从jdk1.8开始,引入了lambda,即函数式编程。那么如和快速入门并使用这种lambda表达式呢?

为了方便函数式编程,jdk中同时引入了函数式接口,其本质也是接口,特点是有且只有一个抽象方法(可能存在其他非抽象方法)。例如:

@FunctionalInterface
public interface Runnable {
    /**
     * When an object implementing interface <code>Runnable</code> is used
     * to create a thread, starting the thread causes the object's
     * <code>run</code> method to be called in that separately executing
     * thread.
     * <p>
     * The general contract of the method <code>run</code> is that it may
     * take any action whatsoever.
     *
     * @see     java.lang.Thread#run()
     */
    public abstract void run();
}

接口上方存在注解@FunctionalInterface,强行约束该接口是一个函数式接口(满足其特点)。

使用案例:

public class Testlambda {
  public static void main(String[] args) {
    //正常写法
    /*Thread t = new Thread(new Runnable() {
      @Override
      public void run() {
        System.out.println("Hi!It's easy to use Lambda.");
      }
    })*/
    //Lambda
    Thread t = new Thread(() -> {
      System.out.println("Hi!It's easy to use Lambda.");
    });
    t.start();
  }
}

是不是还蛮简单的^-^

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值