In the Java record handling procedures, there might be a requirement to analyze the nature of the data based on the values. It is such that returning the maximum and minimum values from a collection or a list helps the developer assess the type of values as well as the number of values contained or in the case of sorting the records efficiently.
This tutorial sheds light on the approaches to retrieve the maximum and minimum of a list in Java.
How to Find Max/Min of a List in Java?
The max/min of a list can be fetched via the below-stated approaches:
- “Collections.max()” and “Collections.min()” Methods.
- Java 8 “Streams”.
- “for” Loop.
Approach 1: Find Max/Min of a List Using the Collections Class “max()” and “min()” Methods
The “Collections” class “max()” and “min()” methods are used to find the maximum and minimum values in the list which will be carried out in the below code example:
import java.util.*;
public class Minmaxlist {
public static void main(String[] args) {
List<Integer> x = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
System.out.println("Maximum List Value: " + Collections.max(x));
System.out.println("Minimum List Value: " + Collections.min(x));
}}
In this block of code, create a list having the stated integers. After that, utilize the “Collections” class “max()” and “min()” methods to return the maximum and minimum values in the list, respectively.
Output

Approach 2: Find Max/Min of a List Using the Java 8 Stream
The “min()” and “max()” Stream operations accept the Comparator and are utilized to find the min/max value from a Stream of comparable items, demonstrated as follows:
import java.util.*;public class Minmaxlist {
public static void main(String[] args) {
List<Integer> x = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
System.out.println("Maximum List Value: " + x.stream().max(Integer::compare).get());
System.out.println("Minimum List Value: " + x.stream().min(Integer::compare).get());
}}
The above code, first, creates a list of integers. Next, it creates a stream and applies the “max()” and “min()” methods to fetch the maximum and minimum values from a list, respectively. The “get()” method is used because both the “max()” and “min()” methods return an “Optional<Integer>” containing the respective values.
Output

Approach 3: Find Max/Min of a List Via the “for” Loop
In this particular demonstration, a user-defined function is created and the required functionality is achieved by iterating the passed list via the “for” loop:
import java.util.*;
public class Minmaxlist {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
System.out.println("Maximum List Value: " + fetchMax(list));
System.out.println("Minimum List Value: " + fetchMin(list));
}
public static int fetchMax(List<Integer> nums) {
int max = 0;
for (Integer num : nums) {
if (num >= max) {
max = num;
}}
return max;
}
public static int fetchMin(List<Integer> nums) {
int min = Integer.MAX_VALUE;
for (Integer num : nums) {
if (num < min) {
min = num;
}}
return min;
}}
The above code lines, perform the below-given steps:
- Create a list having the provided integers.
- Now, invoke two different functions(specified later) and pass the list as both the function’s arguments individually.
- After that, define the “fetchMax()” function comprising the parameter referring to the passed list.
- In the function definition, initialize a “max” variable for comparing the passed list values.
- It is such that iterate the list and allocate the iterated element to the “max” variable and then compare each iterated value with the previous one stored in the variable till the maximum value is returned.
- Likewise, declare the “fetchMin()” function that returns the minimum value from the passed list.
- This function compares the list of iterated values by first checking if they are in the range of the integer’s max value.
- After that, it stores each of the iterated list values in the “min” variable and compares the next iterated value with the previous one till the minimum value is retrieved.
Output

Conclusion
The max/min of a list can be fetched via the “Collections.max()” and “Collections.min()” methods, the Java 8 “Streams”, or via the “for” loop. This write-up explained several approaches for finding the maximum and minimum of a list in Java.