Lambda表达式基本上表示函数式接口的实例(具有单个抽象方法的接口称为函数接口)。一个例子是( java.lang.Runnable
).lambda表达式实现唯一的抽象函数
- 允许将功能视为方法参数,或将代码视为数据。
- 可以不属于任何类而创建的函数。
- lambda表达式可以像传递对象一样传递,并根据需要执行。
// Java program to demonstrate lambda expressions // to implement a user defined functional interface. // A sample functional interface (An interface with // single abstract method interface FuncInterface { // An abstract function void abstractFun(int x); // A non-abstract (or default) function default void normalFun() { System.out.println("Hello"); } } class Test { public static void main(String args[]) { // lambda expression to implement above // functional interface. This interface // by default implements abstractFun() FuncInterface fobj = (int x)->System.out.println(2*x); // This calls above lambda expression and prints 10. fobj.abstractFun(5); } }
输出:
语法:
lambda operator -> body
其中lambda操作符可以是:
零参数:
() -> System.out.println("Zero parameter lambda");
一个参数:
(p) -> System.out.println("One parameter: " + p);
如果可以从上下文推断出变量的类型,则不必使用括号
多个参数:
(p1, p2) -> System.out.println("Multiple parameters: " + p1 + ", " + p2);
请注意:Lambda表达式就像函数一样,它们接受参数就像函数一样。
// A Java program to demonstrate simple lambda expressions import java.util.ArrayList; class Test { public static void main(String args[]) { // Creating an ArrayList with elements // {1, 2, 3, 4} ArrayList<Integer> arrL = new ArrayList<Integer>(); arrL.add(1); arrL.add(2); arrL.add(3); arrL.add(4); // Using lambda expression to print all elements // of arrL arrL.forEach(n -> System.out.println(n)); // Using lambda expression to print even elements // of arrL arrL.forEach(n -> { if (n%2 == 0) System.out.println(n); }); } }
输出:
注意lambda表达式只能用于实现函数接口。在上面的示例中,lambda表达式还实现了 Consumer
函数接口。
一个Java程序来演示两个参数的lambda表达式的工作。
// Java program to demonstrate working of lambda expressions public class Test { // operation is implemented using lambda expressions interface FuncInter1 { int operation(int a, int b); } // sayMessage() is implemented using lambda expressions // above interface FuncInter2 { void sayMessage(String message); } // Performs FuncInter1's operation on 'a' and 'b' private int operate(int a, int b, FuncInter1 fobj) { return fobj.operation(a, b); } public static void main(String args[]) { // lambda expression for addition for two parameters // data type for x and y is optional. // This expression implements 'FuncInter1' interface FuncInter1 add = (int x, int y) -> x + y; // lambda expression multiplication for two parameters // This expression also implements 'FuncInter1' interface FuncInter1 multiply = (int x, int y) -> x * y; // Creating an object of Test to call operate using // different implementations using lambda Expressions Test tobj = new Test(); // Add two numbers using lambda expression System.out.println("Addition is " + tobj.operate(6, 3, add)); // Multiply two numbers using lambda expression System.out.println("Multiplication is " + tobj.operate(6, 3, multiply)); // lambda expression for single parameter // This expression implements 'FuncInter2' interface FuncInter2 fobj = message ->System.out.println("Hello " + message); fobj.sayMessage("Geek"); } }
输出:
Addition is 9 Multiplication is 18 Hello Geek
要点:
void