Java Streams - DoubleStream findFirst() example








DoubleStream findFirst() returns an OptionalDouble describing the first element of this stream, or an empty OptionalDouble if the stream is empty.

Syntax

findFirst has the following syntax.

OptionalDouble findFirst()

Example

The following example shows how to use findFirst.

import java.util.OptionalDouble;
import java.util.stream.DoubleStream;
//from   w  ww .j  av  a 2s .com
public class Main {
  public static void main(String[] args) {
    DoubleStream b = DoubleStream.of(1.1,2.2,3.3,4.4,5.5);
    OptionalDouble d = b.findFirst();
    if(d.isPresent()){
      System.out.println(d.getAsDouble());
    }else{
      System.out.println("no data");
    }    
  }
}

The code above generates the following result.