Can you use Math.max with an array?
No, but...
If you're using Java 8, you can use streams:
Arrays.stream(array).max().getAsInt()
Otherwise you can write a simple utility method to do it for you:
public static int max(int... array) {
if (array.length == 0) {
// ...
}
int max = array[0];
for (int a : array) {
if (a > max)
max = a;
}
return max;
}
Answer from arshajii on Stack OverflowCan you use Math.max with an array?
No, but...
If you're using Java 8, you can use streams:
Arrays.stream(array).max().getAsInt()
Otherwise you can write a simple utility method to do it for you:
public static int max(int... array) {
if (array.length == 0) {
// ...
}
int max = array[0];
for (int a : array) {
if (a > max)
max = a;
}
return max;
}
// Initializing array of integers
Integer[] num = { 2, 4, 7, 5, 9 };
// using Collections.max() to find minimum element
// using only 1 line.
int max = Collections.max(Arrays.asList(num));
Videos
What is the purpose of the Math.max() function in Java?
Can Math.max() be used with arrays or collections directly?
What data types does Math.max() support?
These function expect just two arguments. If you want the minimum of an array you can use IntStream.
int[] a = { 1, 5, 6 };
int max = IntStream.of(a).max().orElse(Integer.MIN_VALUE);
int min = IntStream.of(a).min().orElse(Integer.MAX_VALUE);
You can simply used in-build java Collection and Arrays to sort out this problem. You just need to import them and use it.
Please check below code.
import java.util.Arrays;
import java.util.Collections;
public class getMinNMax {
public static void main(String[] args) {
Integer[] num = { 2, 11, 55, 99 };
int min = Collections.min(Arrays.asList(num));
int max = Collections.max(Arrays.asList(num));
System.out.println("Minimum number of array is : " + min);
System.out.println("Maximum number of array is : " + max);
}
}
Use Collections with your code using it you can find minimum and maximum .
following is the example code for that:
List<Integer> list = Arrays.asList(100,2,3,4,5,6,7,67,2,32);
int min = Collections.min(list);
int max = Collections.max(list);
System.out.println(min);
System.out.println(max);
Output:
2
100
int[] convertedValues = new int[10];
int max = convertedValues[0];
for (int i = 1; i < convertedValues.length; i++) {
if (convertedValues[i] > max) {
max = convertedValues[i];
}
}
Similarly find for the minimum value by changing lesser symbol.
Using Commons Lang (to convert) + Collections (to min/max)
import java.util.Arrays;
import java.util.Collections;
import org.apache.commons.lang.ArrayUtils;
public class MinMaxValue {
public static void main(String[] args) {
char[] a = {'3', '5', '1', '4', '2'};
List b = Arrays.asList(ArrayUtils.toObject(a));
System.out.println(Collections.min(b));
System.out.println(Collections.max(b));
}
}
Note that Arrays.asList() wraps the underlying array, so it should not be too memory intensive and it should not perform a copy on the elements of the array.
You can simply use the new Java 8 Streams but you have to work with int.
The stream method of the utility class Arrays gives you an IntStream on which you can use the min method. You can also do max, sum, average,...
The getAsInt method is used to get the value from the OptionalInt
import java.util.Arrays;
public class Test {
public static void main(String[] args){
int[] tab = {12, 1, 21, 8};
int min = Arrays.stream(tab).min().getAsInt();
int max = Arrays.stream(tab).max().getAsInt();
System.out.println("Min = " + min);
System.out.println("Max = " + max)
}
}
==UPDATE==
If execution time is important and you want to go through the data only once you can use the summaryStatistics() method like this
import java.util.Arrays;
import java.util.IntSummaryStatistics;
public class SOTest {
public static void main(String[] args){
int[] tab = {12, 1, 21, 8};
IntSummaryStatistics stat = Arrays.stream(tab).summaryStatistics();
int min = stat.getMin();
int max = stat.getMax();
System.out.println("Min = " + min);
System.out.println("Max = " + max);
}
}
This approach can give better performance than classical loop because the summaryStatistics method is a reduction operation and it allows parallelization.