-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestLambda4.java
More file actions
104 lines (83 loc) · 2.57 KB
/
Copy pathTestLambda4.java
File metadata and controls
104 lines (83 loc) · 2.57 KB
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
/*
* Copyright 2021 tu.cn All right reserved. This software is the
* confidential and proprietary information of tu.cn ("Confidential
* Information"). You shall not disclose such Confidential Information and shall
* use it only in accordance with the terms of the license agreement you entered
* into with Tu.cn
*/
package com.ariix.java8.lambda;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
/**
* Java8 内置的四大函数是接口
* <p>
* 1. Consumer<T> 消费型接口
* void accept(T t)
* 2. Supplier<T> 供给型接口
* T get();
* <p>
* 3.Function<T,R> 函数式接口
* R apply(T t)
* 4. Predicate<T> 断言型接口
* boolean test(T t);
*/
public class TestLambda4 {
//Consumer<T> 消费型接口
@Test
public void test() {
happy(100000d, x -> System.out.println("购物 花了" + x + "元"));
}
public void happy(double money, Consumer<Double> consumer) {
consumer.accept(money);
}
public List<Integer> getNumList(int number, Supplier<Integer> supplier) {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < number; i++) {
list.add(supplier.get());
}
return list;
}
// * 2. Supplier<T> 供给型接口
@Test
public void test2() {
Random random = new Random();
List<Integer> numbers = getNumList(20, () -> {
return random.nextInt(100);
});
System.out.println(numbers);
}
// * 3.Function<T,R> 函数式接口
public Integer strHandler(String str, Function<String, Integer> function) {
return function.apply(str);
}
@Test
public void test3() {
Integer length = strHandler("aaaaaaaaaaaa", x -> x.length());
System.out.println(length);
}
// * 4. Predicate<T> 断言型接口
public List<String> filterStr(List<String> list, Predicate<String> predicate) {
List<String> resultList = new ArrayList<>();
for (String s : list) {
if (predicate.test(s)) {
resultList.add(s);
}
}
return resultList;
}
@Test
public void testPredicate() {
List<String> list = new ArrayList<>();
list.add("happy");
list.add("ajiyy");
list.add("jp");
List<String> filterList = filterStr(list, x -> x.contains("a"));
System.out.println(filterList);
}
}