Computing maximum and minimum

Computing the maximum value of an array of numbers can be implemented by looping the array and tracking the maximum value via a comparison with each element of the array. In terms of lines of code, this can be written as follows:

public static int max(int[] arr) {

int max = arr[0];

for (int elem: arr) {
if (elem > max) {
max = elem;
}
}

return max;
}

A little pinch in readability here may entail using the Math.max() method instead of an if statement:

...
max = Math.max(max, elem);
...

Let's suppose that we have the following array of integers and a utility class named MathArrays that contains the preceding methods:

int[] integers = {2, 3, 4, 1, -4, 6, 2};

The maximum of this array can easily be obtained as follows:

int maxInt = MathArrays.max(integers); // 6

In Java 8 functional style, the solution to this problem entails a single line of code:

int maxInt = Arrays.stream(integers).max().getAsInt();
In the functional-style approach, the max() method returns an OptionalInt. Similarly, we have OptionalLong and OptionalDouble.

Furthermore, let's assume an array of objects, in this case, an array of Melon:

Melon[] melons = {
new Melon("Horned", 1500), new Melon("Gac", 2200),
new Melon("Hami", 1600), new Melon("Gac", 2100)
};

public class Melon implements Comparable {

private final String type;
private final int weight;

@Override
public int compareTo(Object o) {
Melon m = (Melon) o;

return Integer.compare(this.getWeight(), m.getWeight());
}

// constructor, getters, equals() and hashCode() omitted for brevity
}

It is obvious that our max() methods defined earlier cannot be used in this case, but the logical principle remains the same. This time, the implementation should rely on Comparable or Comparator. The implementation based on Comparable can be as follows:

public static <T extends Comparable<T>> T max(T[] arr) {

T max = arr[0];

for (T elem : arr) {
if (elem.compareTo(max) > 0) {
max = elem;
}
}

return max;
}

Check the Melon.compareTo() method and note that our implementation will compare the weights of melons. Therefore, we can easily find the heaviest melon from our array as follows:

Melon maxMelon = MathArrays.max(melons); // Gac(2200g)

And the implementation relying on Comparator can be written as follows:

public static <T> T max(T[] arr, Comparator<? super T> c) {

T max = arr[0];

for (T elem: arr) {
if (c.compare(elem, max) > 0) {
max = elem;
}
}

return max;
}

And, if we define a Comparator according to the type of melon, we have the following:

Comparator<Melon> byType = Comparator.comparing(Melon::getType);

Then, we get the maximum melon conforming to the lexicographical comparison of strings:

Melon maxMelon = MathArrays.max(melons, byType); // Horned(1500g)

In Java 8 functional style, the solution to this problem entails a single line of code:

Melon maxMelon = Arrays.stream(melons).max(byType).orElseThrow();
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset