1 // Initializing array of integers
2 Integer[] num = { 2, 4, 7, 5, 9 };
3// using Collections.min() to find minimum element
4 // using only 1 line.
5 int min = Collections.min(Arrays.asList(num));
6
7 // using Collections.max() to find maximum element
8 // using only 1 line.
9 int max = Collections.max(Arrays.asList(num));
1public static double arrayMax(double[] arr) {
2 double max = Double.NEGATIVE_INFINITY;
3
4 for(double cur: arr)
5 max = Math.max(max, cur);
6
7 return max;
8}
1Integer[] num = { 2, 4, 7, 5, 9 };
2 // using Collections.min() to find minimum element
3 // using only 1 line.
4 int min1 = Collections.min(Arrays.asList(num));
5
6 // using Collections.max() to find maximum element
7 // using only 1 line.
8 int max1 = Collections.max(Arrays.asList(num));
9 System.out.println(min1); // 2
10 System.out.println(max1); // 9