Sink.count

Counts all incoming elements until upstream terminates.

Sink operators

Signature

Sink.countSink.count

Description

Counts values emitted from the stream, the count is available through a Future CompletionStage or which completes when the stream completes.

Example

Given a stream of numbers we can count the numbers with the count operator

Scala
sourceval source = Source(1 to 10)
val result = source.runWith(Sink.count)
val count = result.futureValue
println(count)
// will print
// 10
Java
sourceSource<Integer, NotUsed> ints = Source.range(1, 10);
CompletionStage<Long> count = ints.runWith(Sink.count(), system);
count.thenAccept(System.out::println);
// 10

Reactive Streams semantics

completes when upstream completes

backpressures never (counting is a lightweight operation)

cancels never