0

I want to convert a Mono of map to flux . can anyone help me on that please?

The object that I have is something like

Mono<Map<Integer , Project>>

where map key is the project key

From this mono I want to construct a flux of project which is something like

flux<project>

as map key is the project id which is present in the project object, we can ignore that.

I try something like this but that did not work I also do not want to use type conversion directly.

     Flux<Project> projectFluxReturn = (Flux<Project>) result.toStream().collect(
                Collectors.toMap(Project::getProjectId, Function.identity(), (Project project1, Project project2) -> {
                    project1.getTaskList().addAll(project2.getTaskList());
                    project2.getTaskList().clear();
                    return project1;
                })
        )
                .values();
Sujoy
  • 25
  • 3

1 Answers1

0

Have you tried flatMapMany?

In your code should look like this

Flux<Project> projectFluxReturn = result.toStream()
        .collect(Collectors.toMap(Project::getProjectId, Function.identity(), (project1, project2) -> {
            project1.getTaskList().addAll(project2.getTaskList());
            project2.getTaskList().clear();
            return project1;
        }))
        .values()
        .stream()
        //in case you want to flattern the list use .flatMap
        .flatMap(whatever -> Flux.fromIterable(whatever.getWhatever()));

Edit

Mono<Map<Integer, Project>> monoMap = result.toStream();
Flux<Project> fluxProject = monoMap.flatMapMany(map -> Flux.fromIterable(map.values()));

Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148
  • Thank a lot for replying back ... can you please explain Flux.fromIterable(whatever.getWhatever()) by "getWhatever()" what do you mean? – Sujoy May 09 '23 at 10:43
  • In case you want to flattern the thing you have inside `Project`you'll need to do the `flatMap` otherwise just use `.values()` – Skizo-ozᴉʞS ツ May 09 '23 at 13:03
  • if I use .values () only (as i do not want to flatten the Project) this will return me back Collection not the flux is not it? am I missing something? @Skizo-ozᴉʞS – Sujoy May 09 '23 at 15:27
  • my problem is not to flaten the project object , my issue is I have Mono> object which I want to convert into flux format. – Sujoy May 10 '23 at 03:43
  • @Sujoy can you try my edit? – Skizo-ozᴉʞS ツ May 11 '23 at 06:19