Java Streams - Stream min(Comparator comparator) example








Stream min(Comparator<? super T> comparator) returns the minimum element of this stream according to the provided Comparator.

Syntax

min has the following syntax.

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

Example

The following example shows how to use min.

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
//from  w  w  w. j  av a  2  s  . c  o  m
public class Main {
  public static void main(String[] args) {
    List<String> stringList = Arrays.asList("2","1","3","4");

    Optional<String> m = stringList
              .stream()    
              .min(Comparator.reverseOrder());
    if(m.isPresent()){
      System.out.println(m.get());  
    }else{
      System.out.println("No Value");
    }
    
  }
}

The code above generates the following result.