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 max(double[] arr) {
2 double result = arr[0];
3 for (int i = 1; i < arr.length; ++i) {
4 if (result < arr[i]) result = arr[i];
5 }
6 return result;
7}
1public static String max(String[] arr) {
2 String result = arr[0];
3 for (int i = 1; i < arr.length; ++i) {
4 if (result.compareTo(arr[i]) < 0) result = arr[i];
5 }
6 return result;
7}
1public static double max(double[] arr) {
2 double result = Double.NEGATIVE_INFINITY;
3 for (int i = 0; i < arr.length; ++i) {
4 if (result < arr[i]) result = arr[i];
5 }
6 return result;
7}
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
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
10 System.out.println(min1); // 2
11 System.out.println(max1); // 9