Completable Future
In Java CompletableFutures, the methods apply()
, accept()
, and run()
are used to handle the results of a computation or task when it completes. Here's the difference between these methods:
apply()
: This method is used when you want to apply a function to the result of a computation and return a new CompletableFuture with the transformed result. It takes aFunction
as a parameter that specifies how to transform the result. Theapply()
method returns a CompletableFuture that will eventually hold the transformed result.
Example usage:
accept()
: This method is used when you want to perform an action with the result of a computation, but you don't need to return anything. It takes aConsumer
as a parameter that specifies the action to be performed on the result. Theaccept()
method returns a CompletableFuture that completes when the action is done.
Example usage:
run()
: This method is used when you want to perform an action after the completion of a computation, without using the result of the computation. It takes aRunnable
as a parameter that specifies the action to be performed. Therun()
method returns a CompletableFuture that completes when the action is done.
Example usage:
In summary, apply()
is used when you want to transform the result and return a new CompletableFuture, accept()
is used when you want to perform an action with the result, and run()
is used when you want to perform an action without using the result.
Last updated