函数式接口
lambda表达式
Lambda:In programming languages such as Lisp,Python and Ruby lambda is an operator used to denote anonymous functions or closures,following the usage of lambda calculus.
为何需要Lambda表达式
- 在java中,我们无法将函数作为参数传递给一个方法,也无法声明返回一个函数的方法
- 在JavaScript中,函数参数是一个函数,返回值是另一个函数的情况非常常见的;JavaScript是一门非常典型的函数式语言
Lambda表达式作用
- Lambda表达式为Java添加了缺失的函数式编程特性,使我们能将函数当成一等公民看待
- 在将将函数当成一等公民的语言中,Lambda表达式的类型是函数。但在java中,Lambda表达式是对象,他们必须依附于一类特别的对象类型————函数式接口(functional interface)
- 传递行为,而不仅仅是值
关于函数式接口
- 如果一个接口只有一个抽象方法,那么该接口就是一个函数式接口。
- 如果我们在某个接口上声明了FunctionalInterface注解,那么编译器会按照函数式接口的定义来要求该接口。
- 如果某个接口只有一个抽象方法,但我们没有给该接口声明FunctionalInterface注解,那么编译器依旧会将该接口看作是函数式接口
常用函数式接口
|
|
|
|
|
|
|
|
|
|