如何将 ArrayList 转换为 Flux?

use*_*825 9 java-8 spring-webflux

我想将 转换ArrayList<Apple>Flux<Apple>

Java 8 是否提供了 API 来执行此操作?

如果没有,我如何将数组列表转换为通量?

Mic*_*rry 10

I want to convert an ArrayList<Apple> to Flux<Apple>

To answer the question directly - you can use Flux.fromIterable(). Each element in the list will be an element in the flux.

...but I'd also advise caution and rethinking your reasons for doing this here. A flux deals with a reactive stream of elements that are processed individually, a collection is a group of elements all contained within memory at once. If you already have a collection in memory, then you're not really gaining anything by "pretending" it's a reactive stream. You can view it as the equivalent of Flux.just() for collections - often seen in demos, less so in real-world use.

除了快速演示之外,这样做的唯一有效原因是您使用的 API 需要大量元素,但无论出于何种原因,您唯一能够获得的就是列表。