How to Find Maximum Value from a List in Java 8

Learn how to find maximum value from a List in Java 8 using Stream API methods like mapToInt(), reduce(), max(), Integer::compareTo, and Comparator.naturalOrder().

First, we create a list using the Arrays.asList() method. Then, we get the stream from the list using the stream() method.

  1. Using mapToInt() and max()

        The mapToInt() method converts each Integer value from the list into an int value.
        After that, the max() method finds the maximum value from the IntStream .

  1. Using reduce()

        The reduce() method combines all elements of the stream into a single result.
        Here, Integer::max compares two values and returns the greater value.

  1. Using max(Integer::compareTo)

        The Integer::compareTo method compares two integer values.
        The max() method uses this comparison logic to find the greatest value from the list.

  1. Using max(Comparator.naturalOrder())

        The Comparator.naturalOrder() method compares elements in their natural ascending order.
        The max() method returns the highest value from the list.

				
					import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

public class MaxValueTest {
    public static void main(String[] args) {

        List<Integer> list = Arrays.asList(1, 3, 4, 5, 6, 7, 8, 8, 19);

        int maxValue1 = list.stream()
                .mapToInt(Integer::intValue)
                .max()
                .getAsInt();

        System.out.println("Using mapToInt() to fetch max value: " + maxValue1);

        int maxValue2 = list.stream()
                .reduce(Integer::max)
                .get();

        System.out.println("Using reduce() to fetch max value: " + maxValue2);

        int maxValue3 = list.stream()
                .max(Integer::compareTo)
                .get();

        System.out.println("Using compareTo() to fetch max value: " + maxValue3);

        int maxValue4 = list.stream()
                .max(Comparator.naturalOrder())
                .get();

        System.out.println("Using naturalOrder() to fetch max value: " + maxValue4);
    }
}
				
			

Output :
Using mapToInt() to fetch max value: 19
Using reduce() to fetch max value: 19
Using compareTo() to fetch max value: 19
Using naturalOrder() to fetch max value: 19

Different Ways to Find Maximum Value from a List in Java 8

				
					import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;
import java.util.concurrent.atomic.AtomicInteger;

public class Test2 {
    public static void main(String[] args) {

        List<Integer> list = Arrays.asList(1, 3, 4, 5, 6, 7, 8, 8, 19);

        // 1. Using Collections.sort()
        List<Integer> sortedList = new ArrayList<Integer>(list);
        Collections.sort(sortedList);
        int maxValue1 = sortedList.get(sortedList.size() - 1);

        System.out.println("Maximum value using Collections.sort(): " + maxValue1);

        // 2. Using stream().sorted() and findFirst()
        int maxValue2 = list.stream()
                .sorted(Comparator.reverseOrder())
                .findFirst()
                .get();

        System.out.println("Maximum value using sorted(): " + maxValue2);

        // 3. Using forEach() with Lambda
        AtomicInteger maxValue3 = new AtomicInteger(list.get(0));

        list.forEach(num -> {
            if (num > maxValue3.get()) {
                maxValue3.set(num);
            }
        });

        System.out.println("Maximum value using forEach(): " + maxValue3.get());

        // 4. Using traditional for loop
        int maxValue4 = list.get(0);

        for (int i = 1; i < list.size(); i++) {
            if (list.get(i) > maxValue4) {
                maxValue4 = list.get(i);
            }
        }

        System.out.println("Maximum value using for loop: " + maxValue4);

        // 5. Using TreeSet
        TreeSet<Integer> treeSet = new TreeSet<Integer>(list);
        int maxValue5 = treeSet.last();

        System.out.println("Maximum value using TreeSet: " + maxValue5);
    }
}
				
			

Output :
Maximum value using Collections.sort(): 19
Maximum value using sorted(): 19
Maximum value using forEach(): 19
Maximum value using for loop: 19
Maximum value using TreeSet: 19

FAQ
How do you find the minimum value from a List in Java 8?
You can find the minimum value from a List in Java 8 using the Stream API methods such as mapToInt().min(), reduce(Integer::min), or min() with a comparator. These methods process the list elements and return the smallest value.
Which method is best to find maximum value from a List in Java 8?
The mapToInt().max() method is commonly used when working with integer values because it converts the list into an IntStream and directly returns the maximum value.
Can we use reduce() to find the maximum value in Java 8?
Yes, we can use the reduce() method with Integer::max to find the maximum value from a list in Java 8.
If the list is empty, methods like get(), getAsInt(), or max() may throw an exception.
Then use orElse() method for safe handling.below are the program.

				
					import java.util.ArrayList;
import java.util.List;

public class SafeMinValueExample {
    public static void main(String[] args) {

        List<Integer> list = new ArrayList<Integer>();

        int maxValue = list.stream()
                .mapToInt(Integer::intValue)
                .max()
                .orElse(0);

        System.out.println("Maximum value: " + maxValue);
    }
}
				
			

Output :
Maximum value: 0