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.
You can also do this using Java stream api :
double maximum = Arrays.stream(measurements).max();
System.out.println(maximum);
Or a more concise code:
double maximum = Double.MIN_VALUE;
for(double measurement : measurements) {
maximum = Math.max(maximum, measurement);
}
System.out.println(maximum);
Or, sort the array and return the last one
Arrays.sort(measurements);
System.out.println(measurements[measurements.length-1]);
you can try this -
class MaxNumber
{
public static void main(String args[])
{
int[] a = new int[] { 10, 3, 50, 14, 7, 90};
int max = a[0];
for(int i = 1; i < a.length;i++)
{
if(a[i] > max)
{
max = a[i];
}
}
System.out.println("Given Array is:");
for(int i = 0; i < a.length;i++)
{
System.out.println(a[i]);
}
System.out.println("Max Number is:" + max);
}
}