-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFP01Functional.java
More file actions
48 lines (36 loc) · 1.38 KB
/
FP01Functional.java
File metadata and controls
48 lines (36 loc) · 1.38 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
package programming;
import java.util.List;
public class FP01Functional {
public static void main(String[] args) {
List<Integer> numbers = List.of(12, 9, 13, 4, 6, 2, 4, 12, 15);
//printAllNumbersInListFunctional(numbers);
//printEvenNumbersInListFunctional(numbers);
printSquaresOfEvenNumbersInListFunctional(numbers);
}
// private static void print(int number) {
// System.out.println(number);
// }
// private static boolean isEven(int number) {
// return number%2 == 0;
// }
private static void printAllNumbersInListFunctional(List<Integer> numbers) {
// What to do?
numbers.stream().forEach(System.out::println);// Method Reference
}
// number -> number%2 == 0
private static void printEvenNumbersInListFunctional(List<Integer> numbers) {
// What to do?
numbers.stream() // Convert to Stream
.filter(number -> number % 2 == 0) // Lamdba Expression
.forEach(System.out::println);// Method Reference
// .filter(FP01Functional::isEven)//Filter - Only Allow Even Numbers
}
private static void printSquaresOfEvenNumbersInListFunctional(List<Integer> numbers) {
numbers.stream() // Convert to Stream
.filter(number -> number % 2 == 0) // Lamdba Expression
//mapping - x -> x * x
.map(number -> number * number)
.forEach(System.out::println);// Method Reference
// .filter(FP01Functional::isEven)//Filter - Only Allow Even Numbers
}
}