# Lambda表达式

lambda表达式,也可以称为闭包,它是推动java8发布的最重要的新特性。lambda允许把函数作为一个方法的参数(函数作为参数传递进方法中)。使用lambda表达式更简洁紧凑

原理就是一个接口,实例化的时候实现里面接口

# Lambda表达式

  • 例子1:创建线程使用匿名内部类写法

其实就是一个接口,new的时候需要实现里面的方法

new Thread(new Runnable() {
    @Override
    public void run() {
        System.out.println("start thread");
    }
}).start();
1
2
3
4
5
6
  • lambda表达式实现
new Thread(() -> {
    System.out.println("start thread 2");
}).start();
1
2
3
  • 例子 1647529130897.png

  • 代码

package com.warren.blog.test;

import java.util.function.Function;
import java.util.function.IntBinaryOperator;
import java.util.function.IntConsumer;
import java.util.function.IntPredicate;

/**
 * @author warren
 */
public class TestLambda {
    public static void main(String[] args) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("start thread");
            }
        }).start();

        new Thread(() -> {
            System.out.println("start thread 2");
        }).start();

        //binaryOperator
        int c = calculateNum(new IntBinaryOperator() {
            @Override
            public int applyAsInt(int left, int right) {
                return left + right;
            }
        });
        System.out.println(c);

        int d = calculateNum((a, b) -> a + b);
        System.out.println(d);

        //predicate
        printNum(new IntPredicate() {
            @Override
            public boolean test(int value) {
                return value % 2 == 0;
            }
        });

        printNum((int value) -> {
            return value % 2 == 0;
        });

        //lambda 例子3
        Integer a = typeConver(new Function<String, Integer>() {
            @Override
            public Integer apply(String s) {
                return Integer.valueOf(s);
            }
        });
        System.out.println(a);

        String k = typeConver(new Function<String, String>() {
            @Override
            public String apply(String s) {
                return s + "s三更";
            }
        });
        System.out.println(k);

        String q = typeConver((String i) -> {
            return i + "三更";
        });
        System.out.println(q);

        foreachArr(new IntConsumer() {
            @Override
            public void accept(int value) {
                System.out.println(value);
            }
        });

        foreachArr((value) -> {
            System.out.println(value);
        });

    }

    public static int calculateNum(IntBinaryOperator operator) {
        int a = 10;
        int b = 20;
        return operator.applyAsInt(a, b);
    }

    public static void printNum(IntPredicate intPredicate) {
        int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        for (int i : arr) {
            if (intPredicate.test(i)) {
                System.out.println(i);
            }
        }
    }

    public static <R> R typeConver(Function<String, R> function) {
        String str = "1235";
        R result = function.apply(str);
        return result;
    }

    /**
     * 例子5,其中IntConsumer是一个接口
     * 匿名累类部类
     * @param consumer
     */
    public static void foreachArr(IntConsumer consumer) {
        int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        for (int i : arr) {
            consumer.accept(i);
        }
    }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116

# lambda省略规则

  • 参数类型可以省略
  • 方法体只有一句代码时大括号return和唯一句代码的分号可以省略
  • 方法只有一个参数时小括号可以省略
  • 以上这些贵州都记不住也可以省略不记

# 技巧

  • (command+回车(mac) 匿名转化成lambda表达式)
  • (command+z (mac) 撤回)
  • (command+option (mac) 撤回撤回)

# 相关资料

lambda (opens new window)

全量分析

评 论:

上次更新时间: 3/18/2022, 10:08:00 AM