In this section, we will learn what the Stream min() and max() methods are and how to use them in Java.
What is Java Stream min() Method?
The Java Stream min() method is used to get the minimum element in a stream.
This is done by passing a reference to another method as the argument of this method.
This reference method takes the first two elements of the stream and starts its work of finding the smaller element using the values. When it found the smaller element, it will keep that and ignore the one that was greater. After that, it takes the next element in the stream and compares that with the value that was considered the smaller element in the previous comparison. This process goes on until the last element in the stream and so the one that was the smallest between all the comparisons will return as the final result of the min() method.
The way the comparison is done in the reference method is as follows:
The reference method takes two arguments and it will compare the two values to each other.
- If the first argument was bigger than the second one, the return value of the method becomes +1. So this means the second argument should stay for the next comparison, but the first argument should be ignored.
- If the first argument was less than the second one, then the return value of this method becomes -1. This means now the second argument should be ignored and the first argument is the one that moves to the next comparison with the upcoming element in the stream.
- If the first and second arguments are both equal, then one of them will be ignored and the other is passed to the next comparison with the upcoming element in the stream.
Note: the min() method is a terminal and eager method.
Java min() Method Syntax:
Optional<T> min(Comparator<? super T> comparator)
min() Method Parameters:
The method has one parameter and that is of type Comparator, which is a functional interface.
This means we need to pass a reference to a method as the argument of this method.
Here’s the syntax of this reference method:
- The method has two parameters and they are of the same type as the elements in the stream.
- The return value of this reference method is an integer.
min() Method Return Value:
The return value of this method is of type integer and is equal to the minimum element in the target stream.
min() Method Exceptions:
NullPointerException: we get this exception if the minimum element is null.
Example: using Stream min() method
import java.util.stream.Stream; import java.util.Optional; class Main{ public static void main(String[] args) { Stream<Integer> stream = Stream.of(1,2,3,4,5,6,7,8,9,10,3,23,4,23,423,2,34,56,7,86,54,5); Optional<Integer> option = stream.min((v1,v2)->{ if (v1>v2){ return 1; }else if (v1<v2){ return-1; }else{ return 0; } }); if (option.isPresent()){ System.out.println(option.get()); } } }
Output:
1
What is Java Stream max() Method?
The Java Stream max() method is used to get the maximum element in a stream.
This is done by passing a reference to another method as the argument of this method.
This reference method takes the first two elements of the stream and starts its work of finding the greater element using the values. When it found the greater element, it will keep that and ignore the one that was lower. After that, it takes the next element in the stream and compares that with the value that was considered the greatest element in the previous comparison. This process goes on until the last element in the stream and so the one that was the biggest between all the comparisons will return as the final result of the max() method.
The way the comparison is done in the reference method is as follows:
The reference method takes two arguments and it will compare the two values to each other.
- If the first argument was bigger than the second one, the return value of the method becomes +1. So this means the first argument should stay for the next comparison, but the second argument should be ignored.
- If the first argument was less than the second one, then the return value of this method becomes -1. This means now the first argument should be ignored and the second argument is the one that moves to the next comparison with the upcoming element in the stream.
- If the first and second arguments are both equal, then one of them will be ignored and the other is passed to the next comparison with the upcoming element in the stream.
Note: the max() method is a terminal and eager method.
Java max() Method Syntax:
Optional<T> max(Comparator<? super T> comparator)
max() Method Parameters:
The method has one parameter and that is of type Comparator, which is a functional interface.
This means we need to pass a reference to a method as the argument of this method.
Here’s the syntax of this reference method:
- The method has two parameters and they are of the same type as the elements in the stream.
- The return value of this reference method is an integer.
max() Method Return Value:
The return value of this method is the maximum element in the target stream wrapped in an Optional object.
max() Method Exceptions:
The method might throw one exception and that is:
NullPointerException: we get this exception if the maximum element is null.
Example: using Stream max() method
import java.util.stream.Stream; import java.util.Optional; class Main{ public static void main(String[] args) { Stream<Integer> stream = Stream.of(1,2,3,4,5,6,7,8,9,10,3,23,4,23,423,2,34,56,7,86,54,5); Optional<Integer> option = stream.max((v1,v2)->{ if (v1>v2){ return 1; }else if (v1<v2){ return-1; }else{ return 0; } }); if (option.isPresent()){ System.out.println(option.get()); } } }
Output:
423