Lambda 表达式也称为闭包,是匿名类的简短形式。Lambda 表达式简化了单一抽象方法声明接口的使用,因此 lambda 表达式也称为功能接口。Lambda 表达式也称为闭包,是匿名类的简短形式。Lambda 表达式简化了单一抽象方法声明接口的使用,因此 lambda 表达式也称为功能接口。
可以使用 lambda 表达式实现功能接口,无需创建类或匿名类。Lambda 表达式只能用于单一方法声明接口。
Lambda 表达式旨在支持多核处理器架构,这种架构依赖于提供并行机制的软件,而该机制可以提高性能、减少完成时间。
Lambda 表达式具有以下优点:
-
-
简明的语法
方法引用和构造函数引用
相比于匿名类,减少了运行时开销
-
JSR 335 lambda 规范关键点:
A lambda expression is like a method: it provides a list of formal parameters and a body—an expression or block—expressed in terms of those parameters.
A functional interface is an interface that has just one abstract method (aside from the methods of Object
), and thus represents a single function contract.
The formal parameters of a lambda expression may have either declared types or inferred types. These styles cannot be mixed: it is not possible for a lambda expression to declare the types of some of its parameters but leave others to be inferred. Only parameters with declared types can have modifiers.