This post will discuss how to find the minimum value in a list of Integer in Java.

1. Convert List to IntStream

The idea is to convert the list to IntStream and use the IntStream#min method that returns an OptionalInt having the minimum value of the stream. We can either use orElse or orElseGet or orElseThrow to unwrap an OptionalInt to get hold of real Integer inside.

Download  Run Code

2. Using Stream.min() method

We can also use min() provided by Stream, which accepts a Comparator to compare items in the stream against each other and returns an Optional having the minimum value in the stream.

Download  Run Code

 
Since Integer#compare takes two ints as an argument and returns an int value similar to a Comparator#compare, it complies with the Comparator functional interface.

Download  Run Code

3. Reduction Operation

We can also perform a reduction operation on the stream’s values using the Integer#min method, which then returns an Optional describing the minimum value present.

Download  Run Code

 
There’s an overloaded version of the reduce() method that performs a reduction on the values of the stream, using the provided identity value and an associative accumulation method and returns the reduced value.

Download  Run Code

4. Using Collectors

We can also use Collectors to find the minimum value in the list.

1. Collectors#minBy returns a Collector that produces minimal value according to a given Comparator.

Download  Run Code

 
2. Collectors#summarizingInt returns a Collector, which applies an int-producing mapping method to each input value and returns summary statistics for the resulting values containing statistics such as count, min, max, sum, and average.

Download  Run Code

 
3. Collectors#reducing returns a Collector, which performs a reduction of its input values under a specified BinaryOperator and returns an Optional.

Download  Run Code

5. Using Sorting

If we sort the stream in the natural order, then the first value in the stream would be the minimum value. This approach is not recommended as it is very inefficient.

Download  Run Code

 
To avoid NullPointerException if the given list is null and NoSuchElementException if the given list is empty, we can add the following code to all the above-mentioned methods at the beginning:

That’s all about finding the minimum value in an Integer List in Java.

 
References:

1. Stream Javadoc SE 8
2. Collectors Javadoc SE 8