Java Streams - IntStream min() example








IntStream min() returns an OptionalInt describing the minimum element of this stream, or an empty optional if this stream is empty.

Syntax

min has the following syntax.

OptionalInt min()

Example

The following example shows how to use min.

import java.util.OptionalInt;
import java.util.stream.IntStream;
/*from  w ww  .  j  a va 2 s.c o  m*/
public class Main {
  public static void main(String[] args) {
    IntStream i = IntStream.of(6,5,7,1, 2, 3, 3);
    OptionalInt d = i.min();
    if(d.isPresent()){
      System.out.println(d.getAsInt());
    }else{
      System.out.println("no value");  
    }    
  }
}

The code above generates the following result.