grouped
Accumulate incoming events until the specified number of elements have been accumulated and then pass the collection of elements downstream.
Signature
Source.grouped
Source.grouped
Flow.grouped
Flow.grouped
Description
Accumulate incoming events until the specified number of elements have been accumulated and then pass the collection of elements downstream.
See also:
- groupedWeighted for a variant that groups based on element weight
- groupedWithin for a variant that groups based on number of elements and a time window
- groupedWeightedWithin for a variant that groups based on element weight and a time window
Examples
The below example demonstrates how grouped
groups the accumulated elements into Seq
List
and maps with other operation.
- Scala
-
source
Source(1 to 7).grouped(3).runForeach(println) // Vector(1, 2, 3) // Vector(4, 5, 6) // Vector(7) Source(1 to 7).grouped(3).map(_.sum).runForeach(println) // 6 (= 1 + 2 + 3) // 15 (= 4 + 5 + 6) // 7 (= 7)
- Java
-
source
Source.from(Arrays.asList(1, 2, 3, 4, 5, 6, 7)) .grouped(3) .runForeach(System.out::println, system); // [1, 2, 3] // [4, 5, 6] // [7] Source.from(Arrays.asList(1, 2, 3, 4, 5, 6, 7)) .grouped(3) .map(g -> g.stream().reduce(0, Integer::sum)) .runForeach(System.out::println, system); // 6 (= 1 + 2 + 3) // 15 (= 4 + 5 + 6) // 7 (= 7)
Reactive Streams semantics
emits when the specified number of elements has been accumulated or upstream completed
backpressures when a group has been assembled and downstream backpressures
completes when upstream completes
1.1.2