Search code examples
spring-webfluxproject-reactorfluxreactive

Convert the Mono<List<PojoA>> to List<PojoB>


I have a Mono<List<PojoA>> object. I need to iterate the list of PojoA and form a new List<String>

    public List<String> getImageList() {
    
        Mono<List<PojoA>> pojoAListMono = someMethod();
        List<String> list = new ArrayList<String>();
    
        pojoAListMono.flatMapMany(imageList -> {
          imageList.stream().forEach(image -> list.add("/images/" + image.getImageName()));
        });
      }

Solution

  • what are you requesting to do (going from a stream to a list of objects) is not suggested because you lose the power of reactive (async) converting to a blocking state.

    but sure you can do that using.block() below is an

                public List<String> getImageList() {
                   return someMethod()
                   .flatMapIterable(pojoAS -> pojoAS)
                   .map(pojoA -> "/images/"+pojoA.getImageName())
                   .collectList()
                   .block();
                }