Overview
In this article, we will understand Stream.min() method added in java 8 with its use and examples in detail.

Stream.min()

Stream.min() is a new addition to the java 8 API which allows you to find the minimum element in a stream.
Minimum element is determined according to the comparator that is supplied as argument to this function.
There are many ways that Stream.min() can used, as
1. Finding the smallest element in a stream of numbers,
2. Finding the shortest string in a stream of strings,
3. Find an object with minimum value of a field such as an employee with lowest salary.

Java docs for Stream.min() state,

Returns the minimum element of this stream according to the provided Comparator

Syntax
Stream.min() is invoked on a stream object and accepts a comparator as argument.
Minimum element is determined by applying this comparator on stream elements.
min() returns an Optional object containing the minimum value.
Its syntax is

Optional<T> min(Comparator<? super T> comparator);

If no Comparator is passed, then the minimum element is determined by the natural ordering of the elements.

Stream.min() example
To use Stream.min(), first you need to create a stream.
This can be done in many ways
1. Using the Stream.of() method,
2. Using the Collection.stream() method,
3. Calling stream() method on a list.

Once you have a stream, you can then call the Stream.min() method and pass in a Comparator.
The Comparator will be used to determine the minimum element in the stream.
An example to find the minimum integer in a stream of integers is given below

Stream<Integer> numbers = Stream.of(34,56,1,56,3,34); 
Optional<Integer> minNumberOp = numbers.min(
                Comparator.comparing(Integer::valueOf));
int min = minNumberOp.get(); 
System.out.println("Minimum number is: " + min);

Output is

Minimum number is 1

Note that to get the actual minimum value from an Optional, call its get() method.

There are a number of ways you can create a comparator to supply to min() as shown below

// as a lambda expression
Comparator<Integer> c = (i, j)-> i.compareTo(j);

// with natural order method
Comparator.naturalOrder()

// as an anonymous class
new Comparator<Integer>() {
  public int compare(Integer o1, Integer o2) {
    return o1.compareTo(o2);
  }
}

where the first method is the most concise as it uses another java 8 feature, Lambda expressions.

Stream.min() to find shortest word
Below is an example to find the shortest word in a stream of string values.

Stream<String> words = Stream.of("the", "quick", "brown", 
                         "fox", "jumps", "over", "the", 
                         "lazy", "dog");
String shortestWord = words.min(
                      Comparator.comparing(String::length)).
                     get();
System.out.println("The shortest word is: " +  
                     shortestWord);

As before, you can use comparator in any of the multiple ways.
Find min in stream of objects
Below is an example of Stream.min() to find the youngest person in a stream of Person objects.

Stream<Person> persons = Stream.of(
                          new Person("A", 50), 
                          new Person("B", 60), 
                          new Person("C", 40)); 
Person yPerson = people.min(
                          Comparator.comparing(Person::getAge)
                         ).get(); 
System.out.println("Youngest person is: " + yPerson);

Here, Person is a simple java class with two fields: name and age and an overridden toString() method.

Output is

Youngest person is: [Name: C, Age: 40]

Hope the article was useful.