Java Stream min()

The Stream min() method is used to select the minimum/smallest element in the Stream according to the Comparator used for comparing the elements.

The Comparator imposes a total ordering on the Stream elements which may not have a natural ordering.

1. Stream min() Method

1.1. Method Syntax

The method takes a non-interfering, stateless Comparator to compare elements of the stream.

It returns an Optional describing the maximum element of the stream, or an empty Optional if the stream is empty.

The min() method throws NullPointerException if the minimum element found is null.

Optional<T> min(Comparator<? super T> comparator)

1.2. Description

  • This is a terminal operation. So stream cannot be used after this method is executed.
  • Returns the minimum/smallest element of this stream according to the provided Comparator.
  • This is a special case of a stream reduction.
  • The method argument shall be a non-interfering, stateless Comparator.
  • The method returns an Optional describing the smallest element of this stream, or an empty Optional if the stream is empty.
  • It may throw NullPointerException if the smallest element is null.

2. Java Stream min() Example

Example 1: Smallest element in the Stream with Lambda Expression

Java example to find the minimum number from a stream of numbers using comparator as lambda expression.

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

public class Main 
{
	public static void main(String[] args) 
	{
		List<Integer> list = Arrays.asList(2, 4, 1, 3, 7, 5, 9, 6, 8);

		Optional<Integer> minNumber = list.stream()
								.min((i, j) -> i.compareTo(j));

		System.out.println(minNumber.get());
	}
}

Program output.

1

Example 2: Smallest element in stream with comparator

Java example to find the minimum number from a stream of numbers using custom comparator.

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

public class Main 
{
	public static void main(String[] args) 
	{
		List<Integer> list = Arrays.asList(2, 4, 1, 3, 7, 5, 9, 6, 8);
		
		Comparator<Integer> minComparator = new Comparator<Integer>() {
			
			@Override
			public int compare(Integer n1, Integer n2) {
				return n1.compareTo(n2);
			}
		};

		Optional<Integer> minNumber = list.stream()
								.min(minComparator);

		System.out.println(minNumber.get());
	}
}

Program output.

1

Drop me your questions related to Java 8 Stream min() API in Java Stream API to find the smallest element in stream.

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

1 thought on “Java Stream min()”

  1. List integerList = Arrays.asList(5,2,7,6,7,8,33,23,64);
    Optional minInteger = integerList.stream().min(myCustomComparator);
    Consumer action = i -> {
    System.out.println(“Minimum integer is :::::::: ” + i);
    };
    minInteger.ifPresent(action);//Dont know how this action is working with proper out put.

    ///////////////////////////////
    Hi the above snippet is working fine, but could not decipher how “i” is populated with “minInteger” in the action above.

    Reply

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.