Easy Tutorial
❮ Java9 Enhanced Deprecated Annotation Number Log ❯

Java Example - Finding Maximum and Minimum Values in an Array

Java Examples

The following example demonstrates how to find the maximum and minimum values in an array using the Collections.max() and Collections.min() methods from the Collections class:

Main.java File

import java.util.Arrays;
import java.util.Collections;

public class Main {
    public static void main(String[] args) {
        Integer[] numbers = { 8, 2, 7, 1, 4, 9, 5};
        int min = (int) Collections.min(Arrays.asList(numbers));
        int max = (int) Collections.max(Arrays.asList(numbers));
        System.out.println("Minimum value: " + min);
        System.out.println("Maximum value: " + max);
    }
}

The output of the above code is:

Minimum value: 1
Maximum value: 9

Java Examples

❮ Java9 Enhanced Deprecated Annotation Number Log ❯