Ung*_*lan 3 java project-reactor spring-webflux
我对反应式世界还很陌生
我的代码如下所示:
Flux.fromIterable(list)
.collectMap(a -> a.getName(),
b-> functionReturningMonoOfC(b)
.map(C::url)
.block();
Run Code Online (Sandbox Code Playgroud)
结果的类型为Map<String, Mono<String>>。我希望它是 类型的Map<String, String>。有任何想法吗?
flatMap我建议在将元素收集到之前使用运算符Map
public class ReactorApp {
record Person(String name){}
public static Mono<String> functionReturningMono(Person person) {
return Mono.just("Hello " + person.name());
}
public static void main(String[] args) {
List<Person> persons = List.of(
new Person("John"),
new Person("Mike"),
new Person("Stacey")
);
Map<String, String> result = Flux.fromIterable(persons)
.flatMap(person -> functionReturningMono(person)
.map(String::toUpperCase)
.map(message -> Map.entry(person.name(), message)))
.collectMap(Map.Entry::getKey, Map.Entry::getValue)
.block();
System.out.println("Result : " + result);
// Result : {Mike=HELLO MIKE, Stacey=HELLO STACEY, John=HELLO JOHN}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5589 次 |
| 最近记录: |