How to find the min and max value in array with Java

2 Answers

0 votes
import java.util.Arrays;
import java.util.IntSummaryStatistics;
 
public class MyClass {
    public static void main(String args[]) {
        int[] arr = new int [] {3, 4, 9, 2, 7, 8, 5};   

        IntSummaryStatistics summaryStat = Arrays.stream(arr).summaryStatistics();
        
        int min = summaryStat.getMin();
        int max = summaryStat.getMax();
        
        System.out.println("Min = " + min);
        System.out.println("Max = " + max);
    }
}
   
   
   
   
/*
run:
      
Min = 2
Max = 9
      
*/

 



answered Aug 4, 2022 by avibootz
0 votes
import java.util.Arrays;

public class MyClass {
    public static void main(String args[]) {
        int[] arr = new int [] {3, 4, 9, 2, 7, 8, 5};   

        int min = Arrays.stream(arr).min().getAsInt();
        int max = Arrays.stream(arr).max().getAsInt();
        
        System.out.println("Min = " + min);
        System.out.println("Max = " + max);
    }
}
   
   
   
   
/*
run:
      
Min = 2
Max = 9
      
*/

 



answered Aug 4, 2022 by avibootz

Related questions

1 answer 136 views
1 answer 130 views
1 answer 131 views
1 answer 77 views
1 answer 222 views
...