Java Stream min() and max()
May 21, 2020
This page will walk through Java Stream.min
and Stream.max
examples. The java.util.stream.Stream
has been introduced in Java 8. Using Stream.min
method we get the minimum element of this stream for the given comparator. Using Stream.max
method we get the maximum element of this stream for the given comparator. The min
and max
method both are stream terminal operations.
Let us discuss
min
and max
methods with examples.
Contents
1. Stream.min()
It returns the minimum element of this stream according to the providedComparator
. It is the special case of reduction, for example using Stream.reduce
method.
Find the
min
method declaration from Java doc.
Optional<T> min(Comparator<? super T> comparator)
Returns: The method returns
Optional
containing minimum element or empty.
Throws: The method throws
NullPointerException
if minimum element is null.
2. Stream.max()
It returns the maximum element of this stream according to the providedComparator
. It is the special case of reduction, for example using Stream.reduce
method.
Find the
max
method declaration from Java doc.
Optional<T> max(Comparator<? super T> comparator)
Returns: The method returns
Optional
containing maximum element or empty.
Throws: The method throws
NullPointerException
if maximum element is null.
3. Min and Max for String and Integer
Find the example ofmin
and max
for the stream of string and integer.
MinMaxDemo1.java
package com.concretepage; import java.util.Arrays; import java.util.Comparator; import java.util.List; import java.util.Optional; public class MinMaxDemo1 { public static void main(String[] args) { System.out.println("---Min and Max for Integer---"); List<Integer> numList = Arrays.asList(42, 44, 43, 41); Comparator<Integer> comparator = Comparator.comparing(Integer::intValue); Optional<Integer> minOptional = numList.stream().min(comparator); minOptional.ifPresent(e -> System.out.println("Min: " + e)); Optional<Integer> maxOptional = numList.stream().max(comparator); maxOptional.ifPresent(e -> System.out.println("Max: " + e)); System.out.println("---Min and Max for String---"); List<String> list = Arrays.asList("Mohit", "Nilesh", "Shankar", "Brajesh"); list.stream().min(Comparator.comparing(String::valueOf)) .ifPresent(e -> System.out.println("Min: " + e)); list.stream().max(Comparator.comparing(String::valueOf)) .ifPresent(e -> System.out.println("Max: " + e)); } }
---Min and Max for Integer--- Min: 41 Max: 44 ---Min and Max for String--- Min: Brajesh Max: Shankar
min
and max
is the special case of stream reduction. Let us achieve the same using Stream.reduce
method.
MinMaxDemoWithReduce.java
package com.concretepage; import java.util.Arrays; import java.util.List; public class MinMaxDemoWithReduce { public static void main(String[] args) { System.out.println("---Min and Max for Integer---"); List<Integer> numList = Arrays.asList(42, 44, 43, 41); //For min numList.stream().reduce(Integer::min).ifPresent(s -> System.out.println(s)); //41 //For max numList.stream().reduce(Integer::max).ifPresent(s -> System.out.println(s)); //44 System.out.println("---Min and Max for String---"); List<String> list = Arrays.asList("Mohit", "Nilesh", "Shankar", "Brajesh"); //For min list.stream().reduce((s1, s2) -> { if (s1.compareTo(s2) <= 0) { return s1; } return s2; }).ifPresent(s -> System.out.println(s)); //Brajesh //For max list.stream().reduce((s1, s2) -> { if (s1.compareTo(s2) >= 0) { return s1; } return s2; }).ifPresent(s -> System.out.println(s)); //Shankar } }
4. Min and Max for Object
Find the example ofmin
and max
for the stream of object.
MinMaxDemo2.java
package com.concretepage; import java.util.Arrays; import java.util.Comparator; import java.util.List; public class MinMaxDemo2 { public static void main(String[] args) { List<User> users = Arrays.asList( new User("Mahesh", 30), new User("Krishna", 29), new User("Virat", 28) ); System.out.println("---Min and Max on the basis of user name---"); users.stream() .min(Comparator.comparing(u -> u.getUserName())) .ifPresent(e -> System.out.println("Min: " + e.getUserName())); users.stream() .max(Comparator.comparing(u -> u.getUserName())) .ifPresent(e -> System.out.println("Max: " + e.getUserName())); System.out.println("---Min and Max on the basis of age---"); users.stream() .min(Comparator.comparing(User::getAge)) .ifPresent(e -> System.out.println("Min: " + e.getUserName())); users.stream() .max(Comparator.comparing(User::getAge)) .ifPresent(e -> System.out.println("Max: " + e.getUserName())); } } class User { private String userName; private int age; public User(String userName, int age) { this.userName = userName; this.age = age; } //Sets and Gets }
---Min and Max on the basis of user name--- Min: Krishna Max: Virat ---Min and Max on the basis of age--- Min: Virat Max: Mahesh
5. Min and Max for Date
Find the example ofmin
and max
for the stream of date.
MinMaxDemo3.java
package com.concretepage; import java.time.LocalDate; import java.time.temporal.ChronoUnit; import java.util.Arrays; import java.util.Comparator; import java.util.List; public class MinMaxDemo3 { public static void main(String[] args) { LocalDate ld = LocalDate.parse("2020-05-15"); List<LocalDate> ldList = Arrays.asList( ld.minus(10, ChronoUnit.DAYS), //2020-05-05 ld, //2020-05-15 ld.plus(15, ChronoUnit.DAYS) //2020-05-30 ); ldList.stream() .min(Comparator.comparing(LocalDate::toEpochDay)) .ifPresent(e -> System.out.println("Min: " + e)); ldList.stream() .max(Comparator.comparing(LocalDate::toEpochDay)) .ifPresent(e -> System.out.println("Max: " + e)); } }
Min: 2020-05-05 Max: 2020-05-30
6. Using IntStream, LongStream and DoubleStream
Find the Java doc formin
and max
methods of IntStream
, LongStream
and DoubleStream
.
For
IntStream
.
OptionalInt min() OptionalInt max()
LongStream
.
OptionalLong min() OptionalLong max()
DoubleStream
.
OptionalDouble min() OptionalDouble max()
MinMaxDemo4.java
package com.concretepage; import java.util.stream.DoubleStream; import java.util.stream.IntStream; import java.util.stream.LongStream; public class MinMaxDemo4 { public static void main(String[] args) { System.out.println("--- Min and Max for IntStream ---"); IntStream.of(12, 20, 35, 48).min() .ifPresent(i -> System.out.println(i)); IntStream.of(12, 20, 35, 48).max() .ifPresent(i -> System.out.println(i)); System.out.println("--- Min and Max for LongStream ---"); LongStream.of(200, 300, 400).min() .ifPresent(l -> System.out.println(l)); LongStream.of(200, 300, 400).max() .ifPresent(l -> System.out.println(l)); System.out.println("--- Min and Max for DoubleStream ---"); DoubleStream.of(110.54, 230.57, 360.65).min() .ifPresent(l -> System.out.println(l)); DoubleStream.of(110.54, 230.57, 360.65).max() .ifPresent(l -> System.out.println(l)); } }
--- Min and Max for IntStream --- 12 48 --- Min and Max for LongStream --- 200 400 --- Min and Max for DoubleStream --- 110.54 360.65