by BehindJava

How to find the minimum or least value in an Integer List using Java 8

Home » java » How to find the minimum or least value in an Integer List using Java 8

In this tutorial we are going to learn about find the least or minimum value in an Integer List in Java 8.

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.

// Method to find minimum value in a list in Java 8 and above
public static Integer findMin(List<Integer> list)
{
    return list.stream()                        // Stream<Integer>
                .mapToInt(v -> v)               // IntStream
                .min()                          // OptionalInt
                .orElse(Integer.MAX_VALUE);     // Integer
}

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.

// Method to find minimum value in a list in Java 8 and above
public static Integer findMin(List<Integer> list)
{
    return list.stream()                        // Stream<Integer>
                .min(Comparator.naturalOrder()) // Optional<Integer>
                .get();                         // Integer
}

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.

// Method to find minimum value in a list in Java 8 and above
public static Integer findMin(List<Integer> list)
{
    return list.stream()                // Stream<Integer>
                .min(Integer::compare)  // Optional<Integer>
                .get();                 // Integer
}

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.

// Method to find minimum value in a list in Java 8 and above
public static Integer findMin(List<Integer> list)
{
    return list.stream()                // Stream<Integer>
                .reduce(Integer::min)   // Optional<Integer>
                .get();                 // Integer
}

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.

// Method to find minimum value in a list in Java 8 and above
public static Integer findMin(List<Integer> list)
{
    return list.stream()
            .reduce(Integer.MAX_VALUE, Integer::min);
}

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.

    // Method to find minimum value in a list in Java 8 and above
    public static Integer findMin(List<Integer> list)
    {
    return list.stream()
            .collect(Collectors.minBy(Comparator.naturalOrder()))
            .get();
    }
  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.

    // Method to find minimum value in a list in Java 8 and above
    public static Integer findMin(List<Integer> list)
    {
    return list.stream()
                .collect(Collectors.summarizingInt(Integer::intValue))
                .getMin();
    }
  3. Collectors#reducing returns a Collector, which performs a reduction of its input values under a specified BinaryOperator and returns an Optional.
// Method to find minimum value in a list in Java 8 and above
public static Integer findMin(List<Integer> list)
{
    return list.stream()
                .collect(Collectors.reducing(Integer::min))
                .get();
}

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.

// Method to find minimum value in a list in Java 8 and above
public static Integer findMin(List<Integer> list)
{
    return list.stream()
                .sorted()
                .findFirst()
                .get();
}

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:

if (list == null || list.size() == 0) {
    return Integer.MAX_VALUE;
}