我有点困惑,现在我,所以我也应该返回CompletableFuture<List<A>>
CompletableFutures和过滤依据是内部
方法里面的方法值是:
CompletableFuture<List<String>> toReturn = asyncCall().thenApply(....)
.thenCompose(listOfStuff -> convertToList(listOfStuff.stream().map(
key -> asyncCall2(key)
.thenApply(optionalValue -> optionalValue.orElse(null))
).collect(Collectors.toList()));
和convertToList()
只需加入期货CompletableFuture<List<ComputableFuture<A>>>
转换成CompletableFuture<List<A>>
基本上我的意图是,以过滤从optionalValue.orElse(null)
出现空值,而且它会很容易科尔之前做过滤器所有这一切都列在最后一行,但如果我在.collect
之前使用它,它正在完成CompletableFutures
我怀疑在我的代码中可以做很多重构。
编辑:
private<T> CompletableFuture<List<T>> convertToList(List<CompletableFuture<T>> toConvert) {
return CompletableFuture.allOf(toConvert.toArray(new CompletableFuture[toConvert.size()]))
.thenApply(v -> toConvert.stream()
.map(CompletableFuture::join)
.collect(Collectors.toList())
);
}
使用'Stream.of()'和'Stream.empty()'? – akalikin
不要调用'.orElse(null)',那么你不需要测试'null'。所以'convertToList'的参数类型应该是'List >>',它可以在'join'之后执行'.filter(Optional :: isPresent).map(Optional :: get)'... –
Holger
@ Holger是的,这是一个在'convertToList'中进行过滤的选项(可选项或空值),我想尽一切努力,而'convertToList'可以是一个通用实用函数 – Amir