Home > Software engineering >  Convert Flux<Map<K, V>> into List<V>
Convert Flux<Map<K, V>> into List<V>

Time:06-07

I'm wondering how in spring reactor to convert a Flux<Map<K, V> into a List<V>.

CodePudding user response:

In general, for a Flux instance you can do:

List<Object> myList = flux.collectSortedList().block();

CodePudding user response:

A Flux<Map<K, V>> can emit any number of maps (0..infinity values).

final List<V> values = flux
    .flatMapIterable(Map::values) // extract a concatenated list of all values
                                  // it transforms List<Collection<V>> into Collection<V>
                                  // but keeps it wrapped in a Flux instance
    .collectList() // transforms Flux<T> into Mono<List<T>>
    .block(); // blocks indefinitely, until the value is available and returns the value
  • Related