-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathReducing.java
More file actions
31 lines (20 loc) · 770 Bytes
/
Copy pathReducing.java
File metadata and controls
31 lines (20 loc) · 770 Bytes
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
package com.lun.c05;
import java.util.stream.*;
import com.lun.c04.Dish;
import java.util.*;
import static com.lun.c04.Dish.menu;
public class Reducing {
public static void main(String... args) {
List<Integer> numbers = Arrays.asList(3, 4, 5, 1, 2);
int sum = numbers.stream().reduce(0, (a, b) -> a + b);
System.out.println(sum);
int sum2 = numbers.stream().reduce(0, Integer::sum);
System.out.println(sum2);
int max = numbers.stream().reduce(0, (a, b) -> Integer.max(a, b));
System.out.println(max);
Optional<Integer> min = numbers.stream().reduce(Integer::min);
min.ifPresent(System.out::println);
int calories = menu.stream().map(Dish::getCalories).reduce(0, Integer::sum);
System.out.println("Number of calories:" + calories);
}
}