Sink.headOption
Materializes into a Future[Option[T]]
CompletionStage<Optional<T>>
which completes with the first value arriving wrapped in Some
Optional
, or a None
an empty Optional if the stream completes without any elements emitted.
Signature
Sink.headOption
Sink.headOption
Description
Materializes into a Future[Option[T]]
CompletionStage<Optional<T>>
which completes with the first value arriving wrapped in Some
Optional
, or a None
an empty Optional if the stream completes without any elements emitted.
Example
In this example there is an empty source i.e. it does not emit any element and to handle it we have used headOption operator which will complete with None.
- Scala
-
source
val source = Source.empty val result: Future[Option[Int]] = source.runWith(Sink.headOption) result.foreach(println) // None
- Java
-
source
Source<Integer, NotUsed> source = Source.empty(); CompletionStage<Optional<Integer>> result = source.runWith(Sink.headOption(), system); result.thenAccept(System.out::println); // Optional.empty
Reactive Streams semantics
cancels after receiving one element
backpressures never
1.0.1