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.
class consecutiveOnes
{
public int findMaxConsecutiveOnes(int[] nums) { //constructor that accepts as an argument an int array called nums.
int maxHere = 0, max = 0; //int variable declarations initialized with zero.
for (int n : nums) //for each loop. for int n in nums array:
max = Math.max(max, maxHere = n == 0 ? 0 : maxHere +1); //ternary operator. max is assigned the math.max function (different use of max). max is the total max values consecutively. maxHere is the max value per n in the array.
// This says if the larger of the two values (max, maxHere)
// is zero, return zero and overwrite max with zero(this ensures the consecutive counter for 1 is reset), else increment maxHere by 1.
return max; //returns max
}
public static void main (String[] args)
{
consecutiveOnes co = new consecutiveOnes();
int nums[] = {1, 0, 1, 1, 1, 1};
//int n = nums.length;
System.out.println("number of consecutive ones are: " +co.findMaxConsecutiveOnes(nums));
}
}I understand that the Math.max function returns the highest of two values (in this case, the highest of max and maxHere). Included are comments of what I believe is happening. What I don't understand is why the Math.max function is not returning 2 for max when for example, n in the array is 1. Would it not be the case since maxHere is incrementing by 1?
My rationalization is if n = 1, and maxhere +1 or increments by 1, then maxHere = 2 and new max value is 2, but that would not make sense, especially because consecutive count would be 2 at the first run of the loop...
EDIT: I apologize for the confusion. The code runs fine, but I have a limited understanding of why the Math.max function is choosing maxHere = 1 for max and not maxHere = 2 when maxHere = n =1 since maxHere + 1 would execute.
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.