Find max or min value in an array of primitives using Java Last Updated : 09 Apr, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report A simple method is traverse the array. We initialize min and max as first elements and then compare every element (Starting from 2nd) with current min and max. Java // Java program to find min & max in int[] // array without asList() public class MinNMax { public static void main(String[] args) { int[] num = {8, 3, 6, 1, 9}; int min = num[0]; int max = num[0]; for (int i = 1; i < num.length; i++) { if (num[i] < min) min = num[i]; if (num[i] > max) max = num[i]; } System.out.println("Minimum number of array is : " + min); System.out.println("Maximum number of array is : " + max); } } OutputMinimum number of array is : 1 Maximum number of array is : 9 Another method is to use a stream. We create a stream of array elements and then find the max and min of the stream. Java // Java program to find min & max // in int[] array using Stream import java.util.*; public class MinNMax { public static void main(String[] args) { int[] arr = {8, 3, 6, 1, 9}; // Finding min & max using stream int min = Arrays.stream(arr).min().getAsInt(); int max = Arrays.stream(arr).max().getAsInt(); System.out.println("Minimum number of array is : " + min); System.out.println("Maximum number of array is : " + max); } } OutputMinimum number of array is : 1 Maximum number of array is : 9 How to find for Non-Primitives like Integer?To get the minimum or maximum value from the array we can use the Collections.min() and Collections.max() methods. But as this method requires a list type of data we need to convert the array to list first using above explained "aslist()" function.Note: "The array you are passing to the Arrays.asList() must have a return type of Integer or whatever class you want to use", since the Collections.sort() accepts ArrayList object as a parameter. If you use type int while declaring the array you will end up seeing this error: "no suitable method found for min(List<int[]>)" Java // Java code to demonstrate how to // extract minimum and maximum number // in 1 line. import java.util.Arrays; import java.util.Collections; public class MinNMax { public static void main(String[] args) { // Initializing array of integers Integer[] num = { 2, 4, 7, 5, 9 }; // using Collections.min() to // find minimum element // using only 1 line. int min = Collections.min(Arrays.asList(num)); // using Collections.max() // to find maximum element // using only 1 line. int max = Collections.max(Arrays.asList(num)); // printing minimum and maximum numbers System.out.println("Minimum number of array is : " + min); System.out.println("Maximum number of array is : " + max); } } OutputMinimum number of array is : 2 Maximum number of array is : 9 Comment More infoAdvertise with us Next Article Java Program to Find minimum sum of factors of number A Astha Tyagi Improve Article Tags : Java Java-Arrays Java-Array-Programs Practice Tags : Java Similar Reads Min and Max in a List in Java Given an unsorted list of integers, find maximum and minimum values in it. Input : list = [10, 4, 3, 2, 1, 20] Output : max = 20, min = 1 Input : list = [10, 400, 3, 2, 1, -1] Output : max = 400, min = -1Sorting This is least efficient approach but will get the work done. The idea is to sort the lis 5 min read Java Program to Find minimum sum of factors of number Given a number, find minimum sum of its factors. Examples: Input : 12 Output : 7 Explanation: Following are different ways to factorize 12 and sum of factors in different ways. 12 = 12 * 1 = 12 + 1 = 13 12 = 2 * 6 = 2 + 6 = 8 12 = 3 * 4 = 3 + 4 = 7 12 = 2 * 2 * 3 = 2 + 2 + 3 = 7 Therefore minimum su 2 min read Finding minimum and maximum element of a Collection in Java A Collection is a group of individual objects represented as a single unit. Java provides Collection Framework which defines several classes and interfaces to represent a group of objects as a single unit. These are: Finding minimum and maximum element of a Collection can be easily done using the Co 6 min read Get first and last elements from ArrayList in Java Given an array list, find the first and last elements of it. Examples: Input : aList = {10, 30, 20, 14, 2} Output : First = 10, Last = 2 Input : aList = {10, 30, 40, 50, 60} Output : First = 10, Last = 60 The last element is at index, size - 1 and the first element is stored at index 0. If we know h 3 min read Arrays.binarySearch() in Java with Examples | Set 1 In Java, the Arrays.binarySearch() method searches the specified array of the given data type for the specified value using the binary search algorithm. The array must be sorted by the Arrays.sort() method before making this call. If it is not sorted, the results are undefined. Example:Below is a si 3 min read Java Guava | Shorts.min() method with Examples Shorts.min() method of Guava's Shorts Class is used to find the least value present in an array. The value returned by this method is the smallest short value in the specified array. Syntax: public static short min(short... array) Parameters: This method takes a mandatory parameter array which is a 2 min read Like