- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我无法理解 thenApply
和 thenCompose
之间的区别。
那么,有人可以提供一个有效的用例吗?
来自 Java 文档:
thenApply(Function<? super T,? extends U> fn)
Returns a new
CompletionStage
that, when this stage completesnormally, is executed with this stage's result as the argument to thesupplied function.
thenCompose(Function<? super T,? extends CompletionStage<U>> fn)
Returns a new
CompletionStage
that, when this stage completesnormally, is executed with this stage as the argument to the suppliedfunction.
我知道 thenCompose
的第二个参数扩展了 thenApply
没有的 CompletionStage。
有人可以提供一个示例,在这种情况下我必须使用 thenApply
以及何时使用 thenCompose
?
最佳答案
thenApply
如果您有同步映射功能,则使用。
CompletableFuture<Integer> future =
CompletableFuture.supplyAsync(() -> 1)
.thenApply(x -> x+1);
如果您有异步映射函数(即返回
CompletableFuture
的函数),则使用
thenCompose
。然后它将直接返回带有结果的 future ,而不是嵌套的 future 。
CompletableFuture<Integer> future =
CompletableFuture.supplyAsync(() -> 1)
.thenCompose(x -> CompletableFuture.supplyAsync(() -> x+1));
关于java - 可完成的 future | thenApply 与 thenCompose,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43019126/
Java CompletableFuture 继承了 CompletionStage 和 Future 接口。 CompletableFuture.thenApply 继承自 CompletionSt
需要确认一些事情。代码如下: CompletableFuture .supplyAsync(() -> {return doSomethingAndReturnA();}) .then
我正在阅读关于 CompletableFuture 的文档,thenAccept() 的描述是 Returns a new CompletionStage that, when this stage
我有以下代码(来自 my previous question )在远程服务器上安排任务,然后使用 ScheduledExecutorService#scheduleAtFixedRate 轮询是否完成
我无法理解 thenApply 和 thenCompose 之间的区别。 那么,有人可以提供一个有效的用例吗? 来自 Java 文档: thenApply(Function fn) Returns a
假设我有以下代码: CompletableFuture future = CompletableFuture.supplyAsync( () -> 0); thenApply 案例:
我正在学习 CompletableFuture。 我不是在问 the difference between thenApply() and thenCompose() . 相反,我想问一下感觉不对的代
我收到以下错误 [ERROR] AccountServiceResource.java:[165,38] incompatible types: bad return type in lambda e
我是一名优秀的程序员,十分优秀!