Stream.min() returns the minimum element of the stream based on the provided Comparator. A Comparator is a comparison function, which imposes a total ordering on some collection of objects. min() is a terminal operation which combines stream elements and returns a summary result. So, min() is a special case of reduction. The method returns Optional instance.
Optional<T> min(Comparator<? super T> comparator) Where, Optional is a container object which may or may not contain a non-null value and T is the type of objects that may be compared by this comparator
Exception : This method throws NullPointerException if the minimum element is null.
Example 1 : Minimum from list of Integers.
Example 2 : Reverse comparator to get maximum value using min() function.
Example 3 : Comparing strings based on last characters.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.