Minimum and maximum of an array in Java
As we say and repeat, Java provides predefined methods thanks to its object-oriented concept, which makes programming easier for us, but by programming these methods ourselves, it helps us to practice a little and keep our shape. Returning to our main topic, to find the maximum and minimum value in an array of n elements, you need to know that it is necessary to go through the entire array from the first to the last element while comparing the largest/smallest element found so far with the current element.In the examples in this article, We will present the four solutions to find the minimum and maximum value in an array:
- Naïve solution
- Java 8
- Google's Guava Api library
- The Apache Commons library
Here is a Java program that returns the value and position of the smallest value in an array.
public class min_max_array {Runtime:
public static void main(String[] args) {
int maxVal = Integer.MAX_VALUE;
int minVal = Integer.MIN_VALUE;
int array[] = {51, 24, 19, 5, 37, 76, 61, 99, 101, 36};
for (int number:array)
System.out.print(number+" ");
for(int i = 0; i < array.length; i++){
if(array[i] < maxVal)
maxVal = array[i];
if(array[i] > minVal)
minVal = array[i];
}
System.out.print("\nMinimum value = "+maxVal);
System.out.print("\nMaximum value = "+minVal);
}
}
51 24 19 5 37 76 61 99 101 36The example first initializes the variableminVal with Integer.MAX_VALUE which is the greater value that int can take. This is done to make sure that the initial value is not accidentally smaller than the first value in the array.
Minimum value = 5
Maximum value = 101
Second, the program goes through the array and compares each value with minVal. If the item in the Table is smaller than minVal So minVal receives the value of this element.
Finally, the minimum value found in the array is displayed.
Java 8
With Java 8, it's easy to find the largest and smallest number. In the first code, we convert an array to a stream, then we call the method Arrays.stream().max() which returns an OptionalInt and contains the largest value found.
The second code uses IntStream, a stream specialized for int primitives, and from here it is possible to call the intStream.of().max() which returns an OptionalInt.
public void max_array_java8 (int[] tab) {
OptionalInt max = Arrays.stream(tab).max();
//or
OptionalInt max = IntStream.of(tab).max();
}
public void min_array_java8 (int[] tab) {
OptionalInt min = Arrays.stream(tab).min();
//or
OptionalInt min = IntStream.of(tab).min();
}
Google's Guava API
Guava's Ints class provides two methodsInts.max() and Ints.min() that return the largest and smallest value respectively. To be able to use this API, download it from the official website of Google Guava.
à
public static int max_array_guava (int[] tab) {
int max = Ints.max(tab);
return max;
}
public static int min_array_guava (int[] tab) {
int min = Ints.min(tab);
return min;
}
Apache Commons
The NumberUtils class of Apache commons gives additional functionality to java and contains two methodsNumberUtils.max() and NumberUtils.min() that return the largest and smallest value respectively. To be able to use this library, download it from the official website of the api Apache Commons.public static int max_array_apache (int[] tab) {References:
int max = NumberUtils.max(tab);
return max;
}
public static int min_array_apache (int[] tab) {
int min = NumberUtils.min(tab);
return min;
}
Find Min & Max in an Array Using Minimum Comparisons