Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content
HowToDoInJava
  • Java
  • Spring AI
  • Spring Boot
  • Hibernate
  • JUnit 5
  • Interview

Java Stream min()

Learn to use Stream min() method to select the smallest element in the stream according to the comparator provided in its argument.

Lokesh Gupta

March 29, 2022

Java 8
Java 8 Stream
java_8_lambdas

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. Stream min() Examples

Example 1: Finding Smallest Element with Lambda Expression

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

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: Finding Smallest Element with Comparator

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

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 !!

Comments

Subscribe
Notify of
1 Comment
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

Java 8 Tutorial

  • Java 8 Features
  • Java 8 forEach
  • Java 8 Stream
  • Java 8 Boxed Stream
  • Java 8 Lambda Expression
  • Java 8 Functional Interface
  • Java 8 Method Reference
  • Java 8 Default Method
  • Java 8 Optional
  • Java 8 Predicate
  • Java 8 Regex as Predicate
  • Java 8 Date Time
  • Java 8 Iterate Directory
  • Java 8 Read File
  • Java 8 WatchService
  • Java 8 String to Date
  • Java 8 Difference Between Dates
  • Java 8 Join Array
  • Java 8 Join String
  • Java 8 Exact Arithmetic
  • Java 8 Comparator
  • Java 8 Base64
  • Java 8 SecureRandom
  • Internal vs External Iteration

Table of Contents

  • 1. Stream min() Method
    • 1.1. Method Syntax
    • 1.2. Description
  • 2. Stream min() Examples
    • Example 1: Finding Smallest Element with Lambda Expression
    • Example 2: Finding Smallest Element with Comparator
Photo of author

Lokesh Gupta

A fun-loving family man, passionate about computers and problem-solving, with over 15 years of experience in Java and related technologies. An avid Sci-Fi movie enthusiast and a fan of Christopher Nolan and Quentin Tarantino.
Follow on Twitter Portfolio

Previous

Java Stream max()

Next

Java Stream map()

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Tutorial Series

OOP

Regex

Maven

Logging

TypeScript

Python

Meta Links

About Us

Advertise

Contact Us

Privacy Policy

Our Blogs

REST API Tutorial

Follow On:

  • Github
  • LinkedIn
  • Twitter
  • Facebook
Copyright © 2026 | Sitemap