简体   繁体   中英

reactor java How to add a Mono<String> to a Mono<List<String>>

I'm a newbie to project reactor. I have a question, How to add a Mono to a Mono<List>?(I know I can use Mono.block method to get String from Mono, but I want to learn the correct reactive way, so Mono.block is not accepted.)

import java.util.ArrayList;
import java.util.List;
import reactor.core.publisher.Mono;

public class Classroom {

    private Mono<List<String>> classroom = Mono.just(new ArrayList<String>());
    
    public void addStudent(Mono<String> student) {
        //add the Mono<String> student to Mono<List<String>> classroom
    }
}

You can use mergeWith() operator to merge Mono<String> with the Mono<List<String> :

public static void addStudent(Mono<String> student) {
    classroom = classroom.flatMapMany(Flux::fromIterable).mergeWith(student).collectList();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM