我接受了一些我不知道的课程。其中一些显示为示例:
class Paper {}
class Bakery {}
class Cake extends Bakery {}
class ReflexiveBaker {
/**
* Create bakery of the provided class.
*
* @param order class of bakery to create
* @return bakery object
*/
public Object bake(Class order) {
// Add implementation here
}
}
Run Code Online (Sandbox Code Playgroud)
任务是根据需要重新设计方法签名类型并添加实现。烘焙方法应符合以下要求:
无论我尝试过什么,我都会收到一个错误: Main.java:: error: incompatible types: Object cannot be convert to Cake Cake cake = baker.bake(Cake.class);
我想出的最好的是:
public Object bake(Class<? extends Bakery> order) …Run Code Online (Sandbox Code Playgroud) 我目前正在开发一个涉及一些反应式编程的项目。
我有 4 个不同的反应性存储库,分别从中得到 4 个不同的Mono<List<SomeType>>回报。目标是将它们组合成一个Mono<List<GeneralType>>,以便将其合并到自定义响应中以在ResponseEntity.ok(). 我已经创建了一个GeneralType并成功转换了一个Mono<List<SomeType>>,但是没有取得进一步的进展。
所有存储库都有相似的签名:
public Mono<List<SomeType>> findAllByUserId(UUID userId)
Run Code Online (Sandbox Code Playgroud)
我的回复中的字段将所有不同的列表合并为一个:
private Mono<List<GeneralType>> items;
Run Code Online (Sandbox Code Playgroud)
到目前为止我的方法是什么样的:
public Mono<List<GeneralType>> combineMonos(UUID userId) {
Mono<List<GeneralType>> combo1 = reactiveRepository.findAllByUserId(userId)
.map(list -> list.stream()
.map(GeneralType::new)
.collect(Collectors.toList()));
return combo1; // works just fine
}
Run Code Online (Sandbox Code Playgroud)
所有其他列表都有几乎相同的方法,但是将它们组合成一个 Mono<List> 是一个问题。
我尝试过以下方法:
return Flux.merge(combo1.flatMapMany(Flux::fromIterable), combo2.flatMapMany(Flux::fromIterable)).collectList();
Run Code Online (Sandbox Code Playgroud)
但这样一来,IDE 就会敦促将返回类型更改为Flux<Object>. 另外,有些列表可能为空,所以我不确定zip()这里是否有一个选项。我读过,如果至少有一个结果为空,它将返回所有内容为空。
所以问题是如何在不到处都是block()的情况下以有效的方式完成这一点?