将 Flux 拆分为包含 Reactor 中最多 20 个元素的列表

Split Flux into lists containing up to 20 elements in Reactor

我想将 Flux 分成多个 List(或子 Flux),每个 Flux 最多包含 20 个元素。

我的流量:

"a","s","x"...

转换为: List<String>, List<String> 要么 Flux<String>, Flux<String> - 每个最多包含 20 个元素。

在我看来,Flux.window(20) returning Flux<Flux<T>> 就是您要找的东西。检查其 reference documentation 可以阅读的内容:

Split this Flux sequence into multiple Flux windows containing maxSize elements (or less for the final window) and starting from the first item. Each Flux window will onComplete after maxSize items have been routed.


(感谢@MichaelBerry 的提示)

还有Flux.buffer(20)会returnFlux<List<T>>。在reference documentation中我们可以读到:

Collect incoming values into multiple List buffers that will be emitted by the returned Flux each time the given max size is reached or once this Flux completes.