Java Stream How to - Reduce with Integer::min








Question

We would like to know how to reduce with Integer::min.

Answer

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
/*from w  w w  .j  a va  2 s . c o m*/
public class Main {
  public static void main(String...args){
    List<Integer> numbers = Arrays.asList(3,4,5,1,2);


    Optional<Integer> min = numbers.stream().reduce(Integer::min);
    min.ifPresent(System.out::println);
    
  }
}

The code above generates the following result.