package org.kodejava.util;
import java.util.Arrays;
import java.util.Collections;
public class ArrayMinMax {
public static void main(String[] args) {
// Creates an array of integer numbers in it.
Integer[] numbers = {8, 2, 6, 7, 0, 1, 4, 9, 5, 3};
// To get the minimum or maximum value from the array we can
// use the Collections.min() and Collections.max() methods.
// But as this method requires a list type of data we need
// to convert the array to list first.
int min = Collections.min(Arrays.asList(numbers));
int max = Collections.max(Arrays.asList(numbers));
// Viola! we get the minimum and the maximum value from the
// array.
System.out.println("Min number: " + min);
System.out.println("Max number: " + max);
}
}
And here are the results:
Min number: 0
Max number: 9
Latest posts by Wayan (see all)
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024