List<ComputableFuture<T>>到ComputableFuture<List<T>&gt。

问题描述 投票:0回答:1

给定一个期货的集合,例如list>,我们如何能把它们合并成一个期货图,而不在Kotlin中阻塞。

澄清一下我想做的事情:将T的可完成未来的列表变成可计算的未来,有一个T的列表。

kotlin collections future
1个回答
1
投票

我想你可以用这样的方法。

val futures: List<CompletableFuture<Int>> = (1..10).map { CompletableFuture.supplyAsync { it } }
val results: CompletableFuture<List<Int>> = CompletableFuture.allOf(*futures.toTypedArray())
    .thenApply { futures.map { it.join() } }
© www.soinside.com 2019 - 2024. All rights reserved.