pixel
pixel

Reputation: 26441

Generate two elements from one in Flux?

I'm having following Flux:

val myFlux = Flux.just("a", "b", "duck", "c", "d")

I would like to transform it in a way that each occurrence of duck will produce two elements (eg. Leg)

So:

myMyFlux.magicTransformation()

should produce:

"a", "b", "leg", "leg", "c", "d"

Upvotes: 0

Views: 190

Answers (1)

Michi Gysel
Michi Gysel

Reputation: 780

You can use flatMap to map duck into two elements.

val myFlux = Flux.just("a", "b", "duck", "c", "d")

myFlux.flatMap {
    if (it == "duck") {
        Flux.just("leg", "leg")
    } else {
        Mono.just(it)
    }
}.subscribe(System.out::println)

Upvotes: 1

Related Questions