-
Notifications
You must be signed in to change notification settings - Fork 0
/
FP06Excersise.java
42 lines (29 loc) · 1.21 KB
/
FP06Excersise.java
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
package com.ysingh.functional;
import java.util.Arrays;
import java.util.List;
public class FP06Excersise {
public static void main(String args[]) {
List<Integer> numbers = Arrays.asList(12,9,13,5,4,2,4,12,15);
System.out.println("Square Each Number and Print Sum:");
int sum = squareNumbersInListAndPrintSum(numbers);
System.out.println(sum);
System.out.println();
System.out.println("Cube Each Number and Print Sum:");
sum = cubeNumbersInListAndPrintSum(numbers);
System.out.println(sum);
System.out.println();
System.out.println("Sum of Odd Numbers in the list:");
sum = sumOfOddNumbersInList(numbers);
System.out.println(sum);
System.out.println();
}
private static int squareNumbersInListAndPrintSum(List<Integer> numbers) {
return numbers.stream().map(number -> number * number).reduce(0, (num1, num2) -> num1 + num2);
}
private static int cubeNumbersInListAndPrintSum(List<Integer> numbers) {
return numbers.stream().map(number -> number * number * number).reduce(0, (num1, num2) -> num1 + num2);
}
private static int sumOfOddNumbersInList(List<Integer> numbers) {
return numbers.stream().filter(number -> number % 2 != 0).reduce(0, (num1, num2) -> num1 + num2);
}
}