是否有来自List< CompletionStage>的转换器?到CompletionStage< List>在Java中? [英] Is there a converter from List<CompletionStage> to CompletionStage<List> in Java?

查看:120
本文介绍了是否有来自List< CompletionStage>的转换器?到CompletionStage< List>在Java中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就像在使用 of 的假设示例中一样:

Like in this hypothetical example of using of:

List<CompletionStage<Long>> listOfFutureLongs = getFutureLongs(...)
CompletionStage<List<Long>> futureListOfLongs = CompletionStage.of(listOfFutureLongs)


推荐答案

Internet说,使用可完成的将来:

Internet says, use completable future:

List<CompletionStage<Long>> futureLongs = getFutureLongs();
var arr = futureLongs.toArray(new CompletableFuture[futureLongs.size()]);
CompletionStage<List<Long>> result = CompletableFuture.allOf(arr)
  .thenApply(unused -> futureLongs.stream().map(f -> f.toCompletableFuture().join()).collect(Collectors.toList()));

这篇关于是否有来自List&lt; CompletionStage&gt;的转换器?到CompletionStage&lt; List&gt;在Java中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆