Java Examples - Get Maximum and Minimum Value from an Array
The following example demonstrates how to find the maximum and minimum values in an array using the Collections.max() and Collections.min() methods of 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 running the above code is:
最小值: 1 最大值: 9Other Extensions
Java Examples