Reputation: 1521
I want to convert an ArrayList<Apple>
to Flux<Apple>
Does Java 8 provide an api to do so ?
If not , how can i convert an arraylist to a flux ?
Upvotes: 10
Views: 6536
Reputation: 72284
I want to convert an
ArrayList<Apple>
toFlux<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.
Quick demonstrations aside, about the only valid reason for doing this is if you're using an API that requires a flux of elements, but the only thing you're able to obtain for whatever reason is a list.
Upvotes: 12