Java 8 Collectors.minBy() and Collectors.maxBy() example

In this source code example, we show how to use Java 8 Collectors.minBy() and Collectors.maxBy() methods with an example.

Java 8 Collectors.minBy() and Collectors.maxBy() example


import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

public class JavaCollectMinMaxEx {

    public static void main(String[] args) {

        var vals = List.of(1, 2, 3, 4, 5, 6, 7);

        // can be replaced with min()
        Optional min = vals.stream().collect(Collectors.minBy(Integer::compareTo));

        // can be replaced with max()
        Optional max = vals.stream().collect(Collectors.maxBy(Integer::compareTo));

        min.ifPresent(val -> System.out.printf("Minimum is %d%n", val));
        max.ifPresent(val -> System.out.printf("Maximum is %d%n", val));
    }
}

Output:

Minimum is 1
Maximum is 7

Comments