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
53 lines (36 loc) · 1.93 KB
/
Copy pathMain.java
File metadata and controls
53 lines (36 loc) · 1.93 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
package modern.challenge;
import java.util.Arrays;
import java.util.Comparator;
public class Main {
public static void main(String[] args) {
// array of integers
int[] integers = {2, 3, 4, 1, -4, 6, 2};
// arrays of melons
Melon[] melons = {new Melon("Horned", 1500), new Melon("Gac", 2200),
new Melon("Hami", 1600), new Melon("Gac", 2100)};
// Comparators
Comparator<Melon> byType = Comparator.comparing(Melon::getType);
System.out.println("Compute maximum of an array:");
System.out.println("----------------------------\n");
// compute maximum
int maxInt1 = MathArrays.maxV1(integers);
System.out.println("Solution based on BasicArrays.maxOfArray() with 'for' and 'if': " + maxInt1);
int maxInt2 = MathArrays.maxV2(integers);
System.out.println("Solution based on BasicArrays.maxOfArray() with 'for' and Math.max(): " + maxInt2);
int maxInt3 = Arrays.stream(integers).max().getAsInt();
System.out.println("Solution based on Java 8, functional-style: " + maxInt3);
Melon maxMelon1 = MathArrays.maxV3(melons, byType);
System.out.println("Solution based on Comparator: " + maxMelon1);
@SuppressWarnings("unchecked")
Melon maxMelon2 = MathArrays.maxV4(melons);
System.out.println("Solution based on Comparable: " + maxMelon2);
Melon maxMelon3 = Arrays.stream(melons).max(byType).orElseThrow();
System.out.println("Solution based on Java 8, functional-style: " + maxMelon3);
System.out.println("\nCompute average of an array:");
System.out.println("----------------------------\n");
double avg1 = MathArrays.average(integers);
System.out.println("Average 1: " + avg1);
double avg2 = Arrays.stream(integers).average().getAsDouble();
System.out.println("Average 2: " + avg2);
}
}