max()
is a NumberUtils
class that is used to find the maximum value among three values or in an array of values. The method is overloaded to support all the primitive types.
NumberUtils
The definition of NumberUtils
can be found in the Apache Commons Lang
package, which we can add to the Maven project by adding the following dependency to the pom.xml
file:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
For other versions of the
commons-lang
package, refer to the Maven Repository.
You can import the NumberUtils
class as follows:
import org.apache.commons.lang3.math.NumberUtils;
public static int max(final int... array)
final int... array
: The list of values.This method returns the maximum value in the list of values.
public static int max(int a, final int b, final int c)
import org.apache.commons.lang3.math.NumberUtils;import java.util.Arrays;public class Main{public static void main(String[] args) {int[] values = new int[]{3, 4, 2, 13, 2, 5434, 232, 43233};System.out.printf("NumberUtils.max(%s) = %s", Arrays.toString(values), NumberUtils.max(values));System.out.println();int a = 13;int b = 45;int c = 4;System.out.printf("NumberUtils.max(%s, %s, %s) = %s", a, b, c, NumberUtils.max(a, b, c));}}
3, 4, 2, 13, 2, 5434, 232, 43233]
The method returns 43233
because it is the maximum value in the list of values.
a = 13
b = 45
c = 4
The method returns 45
as it is the maximum value among the three values passed to the method.
The output of the code will be as follows:
NumberUtils.max([3, 4, 2, 13, 2, 5434, 232, 43233]) = 43233
NumberUtils.max(13, 45, 4) = 45
Free Resources