Java 简明教程
Java - Lambda Expressions
Java Lambda Expressions
Lambda expressions 在 Java 8 中引入,并被吹捧为最大的 feature of Java 8。Lambda 表达式促进了函数式编程并极大地简化了开发。Lambda 表达式基于函数式接口的原理。函数式接口是一个 interface ,仅需要一个 method 来实现。Lambda 表达式提供函数式接口方法的实现。
Lambda expressions were introduced in Java 8 and were touted to be the biggest feature of Java 8. Lambda expression facilitates functional programming and simplifies development a lot. A lambda expression works on the principle of functional interface. A Functional interface is an interface with only one method to implement. A lambda expression provides an implementation of the functional interface method.
Lambda expression 极大地简化了函数式编程,使代码可读,且没有任何样板代码。Lambda 表达式可以推断出所用参数的类型,并且可以在没有 return 关键字的情况下返回一个值。对于简单的单语句方法,甚至可以消除花括号。
Lambda expression simplifies functional programming a lot and makes code readable without any boilerplate code. A lambda expression can infer the type of parameter used and can return a value without a return keyword. In the case of the simple one-statement method, even curly braces can be eliminated.
Lambda Expression Syntax
Lambda 表达式的特征在于以下语法。
A lambda expression is characterized by the following syntax.
parameter -> expression body
Characteristics of Java Lambda Expression
以下是 Lambda 表达式的几个重要特征。
Following are the important characteristics of a lambda expression.
-
Optional type declaration − No need to declare the type of a parameter. The compiler can inference the same from the value of the parameter.
-
Optional parenthesis around parameter − No need to declare a single parameter in parenthesis. For multiple parameters, parentheses are required.
-
Optional curly braces − No need to use curly braces in expression body if the body contains a single statement.
-
Optional return keyword − The compiler automatically returns the value if the body has a single expression to return the value. Curly braces are required to indicate that expression returns a value.
Java Lambda Expression Example
在此示例中,我们有一个 MathOperation 函数式接口,它有一个方法 operate,该方法可以接受两个 int 参数,执行操作并以 int 形式返回结果。利用 lambda 表达式,我们创建了 MathOperation operate 方法的四个不同实现,分别用于加、减、乘、除两个整数并获得相对结果。然后,我们有另一个函数式接口 GreetingService,它具有一个方法 sayMessage,我们用它来向控制台打印消息。
In this example, we’ve one functional interface MathOperation with a method operate which can accept two int parameters, perform the operation and return the result as int. Using lambda expression, we’ve created four different implementations of MathOperation operate method to add,subtract,multiply and divide two integers and get the relative result. Then we’ve another functional interface GreetingService with a method sayMessage, which we’ve used to print a message to the console.
package com.tutorialspoint;
public class JavaTester {
public static void main(String args[]) {
JavaTester tester = new JavaTester();
//with type declaration
MathOperation addition = (int a, int b) -> a + b;
//with out type declaration
MathOperation subtraction = (a, b) -> a - b;
//with return statement along with curly braces
MathOperation multiplication = (int a, int b) -> { return a * b; };
//without return statement and without curly braces
MathOperation division = (int a, int b) -> a / b;
System.out.println("10 + 5 = " + tester.operate(10, 5, addition));
System.out.println("10 - 5 = " + tester.operate(10, 5, subtraction));
System.out.println("10 x 5 = " + tester.operate(10, 5, multiplication));
System.out.println("10 / 5 = " + tester.operate(10, 5, division));
//without parenthesis
GreetingService greetService1 = message -> System.out.println("Hello " + message);
//with parenthesis
GreetingService greetService2 = (message) -> System.out.println("Hello " + message);
greetService1.sayMessage("Mahesh");
greetService2.sayMessage("Suresh");
}
interface MathOperation {
int operation(int a, int b);
}
interface GreetingService {
void sayMessage(String message);
}
private int operate(int a, int b, MathOperation mathOperation) {
return mathOperation.operation(a, b);
}
}
Output
让我们编译并运行上述程序,这将生成以下结果 −
Let us compile and run the above program, this will produce the following result −
10 + 5 = 15
10 - 5 = 5
10 x 5 = 50
10 / 5 = 2
Hello Mahesh
Hello Suresh
以下是在上面的示例中需要考虑的几个重要事项。
Following are the important points to be considered in the above example.
-
Lambda expressions are used primarily to define inline implementation of a functional interface, i.e., an interface with a single method only. In the above example, we’ve used various types of lambda expressions to define the operation method of MathOperation interface. Then we have defined the implementation of sayMessage of GreetingService.
-
Lambda expression eliminates the need of anonymous class and gives a very simple yet powerful functional programming capability to Java.
Scope of Java Lambda Expression
使用 lambda 表达式,你可以引用任何 final 变量或有效 final 变量(只分配一次)。如果变量第二次分配了值,lambda 表达式将抛出一个编译错误。
Using lambda expression, you can refer to any final variable or effectively final variable (which is assigned only once). Lambda expression throws a compilation error, if a variable is assigned a value the second time.
Using Constant in Lambda Expression
在这个示例中,我们有一个函数接口 GreetingService,其中包含一个方法 sayMessage,我们用该方法向控制台打印一条消息。现在在 Java Tester 类中,我们有一个 final 类字段 salutation,它的值是“Hello! ”。现在在 lambda 表达式中,我们可以使用这个 final 字段而不出现任何错误。
In this example, we’ve one functional interface GreetingService with a method sayMessage, which we’ve used to print a message to the console. Now in Java Tester class, we’ve one final class field salutation having a value "Hello! ". Now in lambda expression, we can use this field being final without any error.
Example to Use Constant in Lambda Expression
public class JavaTester {
final static String salutation = "Hello! ";
public static void main(String args[]) {
GreetingService greetService1 = message -> System.out.println(salutation + message);
greetService1.sayMessage("Mahesh");
}
interface GreetingService {
void sayMessage(String message);
}
}
让我们编译并运行上述程序,这将生成以下结果 −
Let us compile and run the above program, this will produce the following result −
Hello! Mahesh
Using Lambda Expression in Collections
从 Java 8 开始,几乎所有 collections 都经过了增强,可以接受 lambda 表达式对它们执行操作。例如,迭代列表、筛选列表、对列表排序等。在本示例中,我们将展示如何迭代字符串列表并打印所有元素,以及如何使用 lambda 表达式仅打印列表中的偶数。
From Java 8 onwards, almost all collections are enhanced to accept lambda expression to perform operations on them. For example, to iterate a list, filter a list, to sort a list and so on. In this example, we’re showcasing how to iterate a list of string and print all the elements and how to print only even numbers in a list using lambda expressions.
Example to Use Lambda Expression in Collections
package com.tutorialspoint;
import java.util.ArrayList;
import java.util.List;
public class JavaTester {
public static void main(String args[]) {
// prepare a list of strings
List<String> list = new ArrayList<>();
list.add("java");
list.add("html");
list.add("python");
// print the list using a lambda expression
// here we're passing a lambda expression to forEach
// method of list object
list.forEach(i -> System.out.println(i));
List<Integer> numbers = new ArrayList<>();
numbers.add(1);
numbers.add(2);
numbers.add(3);
numbers.add(4);
numbers.add(5);
numbers.add(6);
numbers.add(7);
numbers.add(8);
System.out.println(numbers);
// filter the list using a lambda expression
// here we're passing a lambda expression to removeIf
// method of list object where we can checking
// if number is divisible by 2 or not
numbers.removeIf( n -> n%2 != 0);
System.out.println(numbers);
}
}
让我们编译并运行上述程序,这将生成以下结果 −
Let us compile and run the above program, this will produce the following result −
java
html
python
[1, 2, 3, 4, 5, 6, 7, 8]
[2, 4, 6, 8]