java-lambda

Java8 Lambda 表达式产生的背景和用法,以及 Lambda 表达式与匿名类的不同等。

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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149

import org.junit.Test;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
* Lambda 学习
*
* <pre>
* 深入浅出 Java 8 Lambda 表达式 http://blog.oneapm.com/apm-tech/226.html
* 关于Java Lambda表达式看这一篇就够了 https://objcoding.com/2019/03/04/lambda/
* Java 8 lambda 最佳实践 https://wizardforcel.gitbooks.io/java8-tutorials/content/Java%208%20lambda%20%E6%9C%80%E4%BD%B3%E5%AE%9E%E8%B7%B5.html
*
* </pre>
*
* @author: weikeqin1@jd.com
* @date: 2019-10-22 20:33
**/
public class LambdaSample {


@Test
public void threadStartOld() {
Worker w = new Worker();
Thread t = new Thread(w);
t.start();
}

@Test
public void threadStartOld2() {
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("Hello from thread");
}
}).start();
}

@Test
public void threadStartNew() {
new Thread(() -> System.out.println("abcd")).start();

// Runnable r = () -> System.out.println("hello world");

}

public void test1() {
Consumer<Integer> c = (x) -> {
System.out.println(x);
};

BiConsumer<Integer, String> b = (Integer x, String y) -> System.out.println(x + " : " + y);

Predicate<String> p = (String s) -> {
return s == null;
};
}


/**
*
*/
@Test
public void arrCompareOld() {
String[] strs = new String[]{"a", "b", "c", "d"};
Arrays.sort(strs, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return o1.compareTo(o2);
}
});
}

/**
*
*/
@Test
public void arrCompareNew() {
String[] strs = new String[]{"a", "b", "c", "d"};
Arrays.sort(strs, (o1, o2) -> {
//return o1.compareTo(o2);
return Integer.compare(o1.length(), o2.length());
});
}

@Test
public void arrCompareNew2() {
String[] strs = new String[]{"a", "b", "c", "d"};
//参数调用其类的相同方法,再简化
Arrays.sort(strs, Comparator.comparingInt(String::length));
}

/**
* 方法的引用
*/
@Test
public void methodUse() {
String[] strs = {"a", "b", "1", "2"};
List<String> list = Arrays.asList(strs);
list.forEach(x -> {
System.out.println(x);
});

/** :: 实际上面这对对象方法的引用 */
List<String> strLen = list.stream().map(String::trim).collect(Collectors.toList());
strLen.forEach(System.out::println);


/** 方法的引用 */
List<String> strLen2 = list.stream().map(s -> s.trim()).collect(Collectors.toList());
strLen2.forEach(x -> {
System.out.println(x);
});
}


@Test
public void calOld() {
//Old way:
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
for (Integer n : list) {
int x = n * n;
System.out.println(x);
}
}

@Test
public void calNew() {
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
list.stream().map((x) -> x * x).forEach(System.out::println);
}

}


class Worker implements Runnable {

@Override
public void run() {
System.out.println("abcd");
}

}

References

[1] 深入浅出 Java 8 Lambda 表达式
[2] 关于Java Lambda表达式看这一篇就够了
[3] Java 8 lambda 最佳实践