DoubleStream findFirst() example

Description

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;
/*  w w  w  .  j  a va 2 s.  c  o  m*/
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.





















Home »
  Java Streams »
    Reference »




Collectors
DoubleStream
DoubleStream.Builder
IntStream
IntStream.Builder
LongStream
LongStream.Builder
Stream
Stream.Builder