BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java 8 Stream Min and Max

Last Updated: September 11, 2022 by Chaitanya Singh | Filed Under: java

In this tutorial, we will learn how to find out the min and max value from a stream of comparable elements such as characters, strings, dates etc.

We use the min() and max() methods to find the min & max value in streams. These methods are used for finding min & max values in different types of streams such as stream of chars, strings, dates etc. We just have to change the parameter that we pass in this method based on the type of stream.

For example:
max(Comparator.comparing(Integer::valueOf)): To get the max value from stream of numbers.
max(Comparator.comparing(LocalDate::toEpochDay)): To get the max date from stream of dates.

As you can see we have used the Comparator.comparing() method to compare elements of stream. This same method is used for comparing elements of all types of streams such as stream of chars, strings, dates etc. Only the parameter will change based on the type of stream.

Finding min and max number from a stream of numbers

In this example we are finding the min and max value from a stream of numbers.

import java.util.Comparator;
import java.util.stream.*;
public class JavaExample{
   public static void main(String args[]) {
		
	//getting max number
	Integer maxnum = Stream.of(10, 13, 4, 9, 2, 100)
	                    .max(Comparator.comparing(Integer::valueOf))
	                    .get();
		 
	//getting min number
	Integer minnum = Stream.of(10, 13, 4, 9, 2, 100)
	                    .min(Comparator.comparing(Integer::valueOf))
	                    .get();
		 
	System.out.println("Max number is: " + maxnum);
	System.out.println("Min number is: " + minnum);
   }
}

Output:
Java 8 Stream min & max number example

Finding min and max date from stream of dates

Lets find the min and max date from a stream of dates using min and max method.

import java.util.Comparator;
import java.time.LocalDate;
import java.util.List;
import java.util.Arrays;
public class JavaExample{
   public static void main(String args[]) {
		
	List<LocalDate> dates = Arrays.asList(LocalDate.now(), 
				LocalDate.now().minusDays(9),
				LocalDate.now().minusMonths(2),
				LocalDate.now().minusDays(30));
		
	//getting max date
	LocalDate maxdate = dates.stream()
              .max( Comparator.comparing( LocalDate::toEpochDay ) )
              .get();
		 
	//getting min date
	LocalDate mindate = dates.stream()
              .min( Comparator.comparing( LocalDate::toEpochDay ) )
              .get();
		 
	System.out.println("Max Date is: " + maxdate);
	System.out.println("Min Date is: " + mindate);
   }
}

Output:
Java 8 Min Max dates

Finding the string with min & max first character

import java.util.Comparator;
import java.util.stream.*;
public class JavaExample{
   public static void main(String args[]) {
	// Get Min or Max String/Char
	String maxFirstChar = Stream.of("Aryan", "Tom", "Harry", "Steve")
			.max(Comparator.comparing(String::valueOf))
			.get();

	String minFirstChar = Stream.of("Aryan", "Tom", "Harry", "Steve")
			.min(Comparator.comparing(String::valueOf))
			.get();

	System.out.println("String with max first char: " + maxFirstChar);
	System.out.println("String with min first char: " + minFirstChar);
   }
}

Output:
Java 8 Min Max

❮ PreviousNext ❯

Top Related Articles:

  1. Java – Date Format to display the Day of the week
  2. How to Call a Method in Java
  3. Java 8 – Calculate days between two dates
  4. Java LocalDate – atStartOfDay() method example
  5. Java Date and Time

Tags: Java8-Features

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap