Lambda表达式
Lambda表达式,也可以称为闭包,是Java 8发布的最重要新特性
- Lambda答应把函数作为一个方法的参数(函数作为参数传递进方法中)
- 使用Lambda表达式可以使代码变的更加简便紧凑
- 语法:
- (parameter) -> expression
- (parameter) -> { statement; }
- parameter: 参数列表,如果只有一个参数,可以省略括号,如果没有参数,也需要使用括号;
- expression 或 { statement; } 是Lambda表达式的主体
我们可以举一个简朴的例子- public class Main {
- public static void main(String[] args) {
- // Lambda表达式计算两数之和
- MathOperation addition = (a, b) -> a + b;
- MathOperation addition2 = (a, b) -> a + b;
- MathOperation addition3 = (a, b) -> a*b;
- System.out.println(addition.operate(5,3));
- System.out.println(addition2.operate(5,3));
- System.out.println(addition3.operate(5,3));
- }
- }
- interface MathOperation{
- int operate(int a, int b);
- }
复制代码 在上面的例子中,MathOperation 是一个函数式接口,它包含一个抽象方法 operation,Lambda 表达式(a, b) -> a + b 实现了这个抽象方法,表示对两个参数举行相加操纵。
所以,Lambda表达式可以用来实现接口的方法,但是只可以实现一种方法,一种以上如下图
- MathOperation operation = (a, b) -> {
- return a / b;
- };
- MathOperation operation = (a, b) -> {
- return a * b + 10;
- };
复制代码 重要特征
简便性
Lambda 表达式提供了一种更为简便的语法,尤其实用于函数式接口。相比于传统的匿名内部类,Lambda 表达式使得代码更为紧凑,淘汰了样板代码的编写。- @FunctionalInterface
- public interface Runnable {
- /**
- * When an object implementing interface Runnable is used
- * to create a thread, starting the thread causes the object's
- * run method to be called in that separately executing
- * thread.
- * <p>
- * The general contract of the method run is that it may
- * take any action whatsoever.
- *
- * @see java.lang.Thread#run()
- */
- public abstract void run();
- }
复制代码- public class Main {
- public static void main(String[] args) {
- //使用匿名内部类
- Runnable runnable1 = new Runnable() {
- @Override
- public void run() {
- System.out.println("Hello World!");
- }
- };
- runnable1.run();
- // 使用 Lambda 表达式
- Runnable runnable2 = () -> System.out.println("Hello World!");
- runnable2.run();
- }
- }
复制代码 函数式编程支持
Lambda 表达式是函数式编程的一种表现,它答应将函数当作参数传递给方法,或者将函数作为返回值,这种支持使得 Java 在函数式编程方面更为灵活,能够更好地处理聚集操纵、并行计算等任务- List<String> names = Arrays.asList("Alice","Bob","Cuiweiyang");
- names.forEach(name -> System.out.println(name));
复制代码 实现了一下接口
变量捕获
Lambda 表达式可以访问外部作用域的变量,这种特性称为变量捕获,Lambda 表达式可以隐式地捕获 final 或究竟上是 final 的局部变量。- int x = 10;
- Function function = a -> System.out.println(a+x);//注意返回值类型
- function.oper(5);
复制代码- interface Function{
- void oper(int a);
- }
复制代码 方法引用
Lambda 表达式可以通过方法引用进一步简化,方法引用答应你直接引用现有类或对象的方法,而不用编写冗余的代码- // 使用方法引用
- List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
- names.forEach(System.out::println);
复制代码 可并行性
Lambda 表达式能够更方便地实现并行操纵,通过使用 Stream API 结合 Lambda 表达式,可以更容易地实现并行计算,提高程序性能。- // 使用 Lambda 表达式和 Stream API 进行并行计算
- List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
- int sum =numbers.parallelStream().mapToInt(Integer::intValue).sum();
- System.out.println("sum: "+sum);
复制代码 Lambda 表达式的引入使得 Java 编程更加灵活、简便,并推动了函数式编程的发展。
Lambda表达式实例
Lambda 表达式的简朴例子:- // 1. 不需要参数,返回值为 5
- () -> 5
- // 2. 接收一个参数(数字类型),返回其2倍的值
- x -> 2 * x
- // 3. 接受2个参数(数字),并返回他们的差值
- (x, y) -> x – y
- // 4. 接收2个int型整数,返回他们的和
- (int x, int y) -> x + y
- // 5. 接受一个 string 对象,并在控制台打印,不返回任何值(看起来像是返回void)
- (String s) -> System.out.print(s)
复制代码 可以分析一下一下代码- public class Java8Tester {
- public static void main(String []args){
- Java8Tester tester = new Java8Tester();
- // 类型声明
- MathOperation addition = (int a, int b) -> a + b;
- // 不用类型声明
- MathOperation subtraction = (a, b) -> a - b;
- // 大括号中的返回语句
- MathOperation multiplication = (int a, int b) -> { return a * b; };
- // 没有大括号及返回语句
- 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));
- // 不用括号
- GreetingService greetService1 = message ->
- System.out.println("Hello " + message);
- // 用括号
- GreetingService greetService2 = (message) ->
- System.out.println("Hello " + message);
- greetService1.sayMessage("Runoob");
- greetService2.sayMessage("Google");
- }
- 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);
- }
- }
复制代码 使用 Lambda 表达式需要注意以下两点:
- Lambda 表达式主要用来定义行内执行的方法范例接口(比方,一个简朴方法接口)。在上面例子中,我们使用各种范例的 Lambda 表达式来定义 MathOperation 接口的方法,然后我们定义了 operation 的执行。
- Lambda 表达式免去了使用匿名方法的麻烦,并且给予 Java 简朴但是强大的函数化的编程能力。
变量作用域
lambda 表达式只能引用标志了 final 的外层局部变量,这就是说不能在 lambda 内部修改定义在域外的局部变量,否则会编译错误。- public class Java8Tester {
- final static String salutation = "Hello! ";
-
- public static void main(String args[]){
- GreetingService greetService1 = message ->
- System.out.println(salutation + message);
- greetService1.sayMessage("Runoob");
- }
-
- interface GreetingService {
- void sayMessage(String message);
- }
- }
复制代码 $ javac Java8Tester.java
$ java Java8Tester
Hello! Runoob
我们也可以直接在 lambda 表达式中访问外层的局部变量:- public class Java8Tester {
- public static void main(String args[]) {
- final int num = 1;
- Converter<Integer, String> s = (param) -> System.out.println(String.valueOf(param + num));
- s.convert(2); // 输出结果为 3
- }
-
- public interface Converter<T1, T2> {
- void convert(int i);
- }
- }
复制代码 lambda 表达式的局部变量可以不用声明为 final,但是必须不可被后面的代码修改(即隐性的具有 final 的语义)- int num = 1;
- Converter<Integer, String> s = (param) -> System.out.println(String.valueOf(param + num));
- s.convert(2);
- num = 5;
- //报错信息:Local variable num defined in an enclosing scope must be final or effectively final
复制代码 在 Lambda 表达式当中不答应声明一个与局部变量同名的参数或者局部变量。- String first = "";
- Comparator<String> comparator = (first, second) -> Integer.compare(first.length(), second.length()); //编译会出错
复制代码 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |