首页 > 技术文章 > Lamdba表达式的代码使用讲解

x-ll123 2017-11-06 16:41 原文

public class Lambda{
    public  static void main(String[] args) {
        repeat(10, (i)->System.out.print("count:"+i));
    }
    public static void repeat(int n, Count count){
        for(int i = 0 ;i<n ;i++){
            count.accept(i);
        }
    }
}
@FunctionalInterface
 interface Count {
     void accept(int value);
 }

以上代码很清楚的可以理解Lamdba表达式为啥要用函数式接口,因为在编译

repeat(10, (i)->System.out.print("count:"+i));语句的时候编译器会通过上下文调用函数接口的抽象方法。

推荐阅读