Skip to content

Commit 4323a7c

Browse files
committed
🍱 add lambda examples
1 parent 0719422 commit 4323a7c

6 files changed

Lines changed: 176 additions & 2 deletions

File tree

java8-lambda/src/main/java/io/github/biezhi/java8/lambda/lesson2/FunctionalDemo.java

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.ArrayList;
44
import java.util.List;
5+
import java.util.UUID;
56
import java.util.function.*;
67

78
/**
@@ -12,6 +13,34 @@
1213
*/
1314
public class FunctionalDemo {
1415

16+
/**
17+
* 断言
18+
*/
19+
public void predicate() {
20+
Predicate<String> namesStartingWithS = name -> name.startsWith("s");
21+
}
22+
23+
/**
24+
* 消费数据
25+
*/
26+
public void consumer() {
27+
Consumer<String> messageConsumer = message -> System.out.println(message);
28+
}
29+
30+
/**
31+
* 转换
32+
*/
33+
public void function() {
34+
Function<String, String> toUpperCase = name -> name.toUpperCase();
35+
}
36+
37+
/**
38+
* 提供数据
39+
*/
40+
public void supplier() {
41+
Supplier<String> uuidGenerator = () -> UUID.randomUUID().toString();
42+
}
43+
1544
public static void main(String[] args) {
1645

1746
List<Integer> list = new ArrayList<>();
@@ -29,10 +58,10 @@ public static void main(String[] args) {
2958
Function<String, String> concat = x -> x + 1;
3059

3160
Integer two = add1.apply(1); //yields 2
32-
String answer = concat.apply("0 + 1 = "); //yields "0 + 1 = 1"
61+
String answer = concat.apply("0 + 1 = "); // "0 + 1 = 1"
3362

3463
BinaryOperator<Integer> sum = (a, b) -> a + b;
35-
Integer res = sum.apply(1, 2); // yields 3
64+
Integer res = sum.apply(1, 2); // 3
3665

3766
BinaryOperator<Function<Integer, Integer>> compose = (f, g) -> x -> g.apply(f.apply(x));
3867

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package io.github.biezhi.java8.lambda.lesson3;
2+
3+
import java.util.function.Supplier;
4+
5+
/**
6+
* 构造器引用
7+
* <p>
8+
* 对于一个现有构造函数, 你可以利用它的名称和关键字 new 来创建它的一个引用: ClassName::new。
9+
* 它的功能与指向静态方法的引用类似。
10+
*
11+
* @author biezhi
12+
* @date 2018/2/10
13+
*/
14+
public class ConstructorReference {
15+
16+
public static void main(String[] args) {
17+
//构造器引用
18+
//根据参数列表自动匹配构造器
19+
Supplier<ConstructorReference> sup = ConstructorReference::new;
20+
ConstructorReference constructorReference = sup.get();
21+
}
22+
23+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package io.github.biezhi.java8.lambda.lesson3;
2+
3+
import java.util.List;
4+
import java.util.function.BiPredicate;
5+
import java.util.function.Function;
6+
7+
/**
8+
* 由你完成的
9+
* <p>
10+
* 下列Lambda表达式的等效方法引用是什么
11+
*
12+
* @author biezhi
13+
* @date 2018/2/10
14+
*/
15+
public class DoneByYou {
16+
17+
public static void main(String[] args) {
18+
Function<String, Integer> stringToInteger = (String s) -> Integer.parseInt(s);
19+
BiPredicate<List<String>, String> contains = (list, element) -> list.contains(element);
20+
21+
}
22+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package io.github.biezhi.java8.lambda.lesson3;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.util.function.Function;
6+
7+
/**
8+
* lambda 中有异常
9+
* <p>
10+
* 任何函数式接口都不允许抛出受检异常
11+
* <p>
12+
* sf上这个问题的一些讨论:https://stackoverflow.com/questions/18198176/java-8-lambda-function-that-throws-exception
13+
*
14+
* @author biezhi
15+
* @date 2018/2/10
16+
*/
17+
public class LambdaException {
18+
19+
public static void main(String[] args) {
20+
Function<BufferedReader, String> f = (BufferedReader b) -> {
21+
try {
22+
return b.readLine();
23+
} catch (IOException e) {
24+
throw new RuntimeException(e);
25+
}
26+
};
27+
}
28+
29+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package io.github.biezhi.java8.lambda.lesson3;
2+
3+
import io.github.biezhi.java8.lambda.lesson1.Project;
4+
5+
import java.util.Arrays;
6+
import java.util.List;
7+
import java.util.function.Predicate;
8+
9+
import static java.util.stream.Collectors.toList;
10+
11+
/**
12+
* 方法引用
13+
* <p>
14+
* 1. 指向静态方法的方法引用
15+
* 2. 指向现有对象的实例方法的方法引用
16+
*
17+
* @author biezhi
18+
* @date 2018/2/10
19+
*/
20+
public class MethodReference {
21+
22+
public static List<Integer> findNumbers(List<Integer> numbers, Predicate<Integer> filter) {
23+
List<Integer> numbersFound = numbers
24+
.stream()
25+
.filter(filter)
26+
.collect(toList());
27+
28+
return numbersFound;
29+
}
30+
31+
public static boolean multipleOf3(Integer number) {
32+
return (number % 3) == 0;
33+
}
34+
35+
public static void main(String[] args) {
36+
List<Integer> numbers = Arrays.asList(1, 3, 6, 8, 9, 12, 14, 15);
37+
38+
List<Integer> multiplesOf3 = findNumbers(numbers, MethodReference::multipleOf3);
39+
System.out.println(multiplesOf3.contains(3));
40+
41+
Project project = Project.builder().name("Blade").build();
42+
Arrays.asList(project).stream()
43+
.map(Project::getName)
44+
.count();
45+
46+
}
47+
48+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package io.github.biezhi.java8.lambda.lesson3;
2+
3+
import java.util.function.Function;
4+
5+
/**
6+
* 数组引用
7+
*
8+
* @author biezhi
9+
* @date 2018/2/10
10+
*/
11+
public class OtherReference {
12+
13+
public static void main(String[] args) {
14+
Function<Integer, String[]> fun = x -> new String[x];
15+
String[] strs = fun.apply(10);
16+
System.out.println(strs.length);
17+
18+
Function<Integer, String[]> fun1 = String[]::new;
19+
strs = fun1.apply(20);
20+
System.out.println(strs.length);
21+
}
22+
23+
}

0 commit comments

Comments
 (0)