forked from PacktPublishing/Java-Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
66 lines (47 loc) · 1.86 KB
/
Copy pathMain.java
File metadata and controls
66 lines (47 loc) · 1.86 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
package modern.challenge;
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Melon> melons = Arrays.asList(new Melon("Gac", 2000),
new Melon("Hemi", 1600), new Melon("Gac", 3000),
new Melon("Apollo", 2000), new Melon("Horned", 1700));
int total1 = melons.stream()
.mapToInt(Melon::getWeight)
.sum();
int total2 = melons.stream()
.map(Melon::getWeight)
.reduce(0, (m1, m2) -> m1 + m2);
System.out.println("The total1 weight is: " + total1);
System.out.println("The total2 weight is: " + total2);
int max1 = melons.stream()
.mapToInt(Melon::getWeight)
.max()
.orElse(-1);
int max2 = melons.stream()
.map(Melon::getWeight)
.reduce(Integer::max)
.orElse(-1);
System.out.println("Max1: " + max1);
System.out.println("Max2: " + max2);
int min1 = melons.stream()
.mapToInt(Melon::getWeight)
.min()
.orElse(-1);
int min2 = melons.stream()
.map(Melon::getWeight)
.reduce(Integer::min)
.orElse(-1);
System.out.println("Min1: " + min1);
System.out.println("Min2: " + min2);
List<Double> numbers = Arrays.asList(1.0d, 5.0d, 8.0d, 10.0d);
double total3 = numbers.stream()
.reduce(1.0d, (x1, x2) -> x1 * x2);
System.out.println("Product: " + total3);
double hm = numbers.size() / numbers.stream()
.mapToDouble(x -> 1.0d / x)
.reduce((x1, x2) -> (x1 + x2))
.orElseThrow();
System.out.println("Harmonic mean: " + hm);
}
}