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.

Answer from Michael Rutherfurd on Stack Overflow
Top answer
1 of 16
203

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.

2 of 16
176

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.

🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-for-program-to-find-largest-element-in-an-array
Java Program to Find Largest Element in an Array - GeeksforGeeks
The most common method to find and print the largest element of a Java array is to iterate over each element of the array and compare each element with the largest value. ... We assume that the first element is the largest and initialize the ...
Published   July 23, 2025
🌐
Programiz
programiz.com › java-programming › library › math › max
Java Math max()
class Main { public static void main(String[] args) { // create an array of int type int[] arr = {4, 2, 5, 3, 6}; // assign first element of array as maximum value int max = arr[0]; for (int i = 1; i < arr.length; i++) {
🌐
Baeldung
baeldung.com › home › java › java array › finding min/max in an array with java
Finding Min/Max in an Array with Java | Baeldung
January 2, 2026 - There are many ways of finding the min or max value in an unordered array, and they all look something like: SET MAX to array[0] FOR i = 1 to array length - 1 IF array[i] > MAX THEN SET MAX to array[i] ENDIF ENDFOR · We’re going to look at how Java 8 can hide these details from us.
🌐
GeeksforGeeks
geeksforgeeks.org › java › find-max-min-value-array-primitives-using-java
Find max or min value in an array of primitives using Java - GeeksforGeeks
April 9, 2025 - // Java program to find min & max ... ... To get the minimum or maximum value from the array we can use the Collections.min() and Collections.max() methods....
🌐
Vultr Docs
docs.vultr.com › java › examples › find-largest-element-of-an-array
Java Program to Find Largest Element of an Array | Vultr Docs
April 10, 2025 - After converting the array to a stream with Arrays.stream(numbers), the max() method computes the maximum element. It returns an OptionalInt, so getAsInt() is used to retrieve the value as an integer—this is a typical method used to find max ...
🌐
BeginnersBook
beginnersbook.com › 2014 › 07 › java-finding-minimum-and-maximum-values-in-an-array
Java – Finding minimum and maximum values in an array
class MinMaxExample { public static void main(String args[]){ int array[] = new int[]{10, 11, 88, 2, 12, 120}; // Calling getMax() method for getting max value int max = getMax(array); System.out.println("Maximum Value is: "+max); // Calling getMin() method for getting min value int min = ...
Find elsewhere
🌐
Delft Stack
delftstack.com › home › howto › java › java find max in array
How to Find the Max Number in an Array in Java | Delft Stack
February 2, 2024 - The IntStream function comes with a method max() that helps to find the maximum value in the stream. It returns an OptionalInt that describes that the stream might have empty int values too.
🌐
AlgoCademy
algocademy.com › link
Max In Array in Java | AlgoCademy
Array with negative numbers: [-1, -2, -3] should return -1. Mixed positive and negative numbers: [1, -2, 3, -4, 5] should return 5. ... Understand the problem requirements and constraints. Think about the most efficient way to solve the problem. Break down the problem into smaller, manageable steps. Consider edge cases and how your solution handles them. Finding the maximum value in an array is a fundamental problem that helps in understanding basic algorithmic concepts.
🌐
DEV Community
dev.to › dhanush9952 › finding-minimum-and-maximum-values-in-an-array-effective-approaches-with-java-programming-39dp
Finding Minimum and Maximum Values in an Array: Effective Approaches with Java Programming - DEV Community
November 6, 2024 - Here, we’ll cover six different methods for finding minimum and maximum values in an array int[] arr = {5, 2, 7, 4, 8, 5, 9, 6}, each with its unique advantages and use cases. This approach leverages Java Streams to find the minimum and maximum values in a concise, readable way.
🌐
W3Schools
w3schools.com › java › java_howto_min_max_array.asp
Java How To Find Min and Max in Array
Java Examples Java Videos Java ... keep track of the highest and lowest values: int[] numbers = {45, 12, 98, 33, 27}; int max = numbers[0]; int min = numbers[0]; for (int n : numbers) { if (n > max) { max = n; } if (n < min) ...
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › the max function in java
Max Function in Java: A Complete Guide with Practice Exercises
March 18, 2025 - During initialization, the program starts by considering arr[0] as the maximum array value. The loop starts at the second element position (i = 1) to check every element against the present maximum value. An element surpassing the present maximum threshold automatically updates the maximum value battle. The function concludes by delivering the highest value which was found throughout all elements. ... In Java 8 and beyond, Streams can be utilized to determine the maximum value within an array or a list.
🌐
DaniWeb
daniweb.com › programming › software-development › threads › 113388 › finding-max-number-in-array
java - Finding Max Number in Array | DaniWeb
Max should be initialized with one element from the array, preferably the first so you can start your loop from i=1: ... When you write: int max=0,i; you declare i to be an int, so you don't need to declare it again in the for loop: ... Try ...
🌐
Baeldung
baeldung.com › home › java › java array › find the index of the largest value in an array
Find the Index of the Largest Value in an Array | Baeldung
June 20, 2024 - As the code shows, we construct an IntStream using the range() method. It’s important to note that this IntStream contains the array’s indexes instead of the array elements. Then, we pass a Comparator to the max() method, which compares the element value (array[i]).
🌐
TestMu AI Community
community.testmu.ai › ask a question
Best Way to Find the Maximum Value in an Array in Java - TestMu AI Community
January 27, 2025 - I know it’s simple to write a function that finds the maximum value in an array of primitives, like this: private static int maxValue(char[] chars) { int max = chars[0]; for (int ktr = 0; ktr max) { max = chars[ktr]; } } return max; ...
🌐
How to do in Java
howtodoinjava.com › home › java array › find max and min in an array in java
Find Max and Min in an Array in Java
February 21, 2023 - In the case of custom objects, we only need to override the equals() method and provide the correct logic to compare two instances. int[] items = { 10, 0, 30, 2, 7, 5, 90, 76, 100, 45, 55 }; // Min = 0, Max = 100