-
Notifications
You must be signed in to change notification settings - Fork 738
Expand file tree
/
Copy pathLambda.java
More file actions
52 lines (42 loc) · 1.33 KB
/
Lambda.java
File metadata and controls
52 lines (42 loc) · 1.33 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
package nextstep.fp;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;
public class Lambda {
public static void printAllOld(List<Integer> numbers) {
System.out.println("printAllOld");
for (int number : numbers) {
System.out.println(number);
}
}
public static void printAllLambda(List<Integer> numbers) {
System.out.println("printAllLambda");
numbers.forEach(System.out::println);
}
public static void runThread() {
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("Hello from thread");
}
}).start();
}
public static int sumAll(List<Integer> numbers) {
return sumAll(numbers, number -> true);
}
public static int sumAllEven(List<Integer> numbers) {
return sumAll(numbers, number -> number % 2 == 0);
}
public static int sumAllOverThree(List<Integer> numbers) {
return sumAll(numbers, number -> number > 3);
}
private static int sumAll(List<Integer> numbers, Predicate<Integer> condition) {
int total = 0;
for (int number : numbers) {
if (condition.test(number)) {
total += number;
}
}
return total;
}
}