completedFuture()
is a static method of the CompletableFuture
class and is used to get a new CompletableFuture
that is in the completed stage, with the passed value as the result of the future.
The completedFuture
method is defined in the CompletableFuture
class. The CompletableFuture
class is defined in the java.util.concurrent
package. To import the CompletableFuture
class, check the following import statement:
import java.util.concurrent.CompletableFuture;
public static <U> CompletableFuture<U> completedFuture(U value)
U value
: The result of the Future
object created.This method returns a new completable future.
import java.util.concurrent.*;public class Main {public static void main(String[] args) throws ExecutionException, InterruptedException {String resultOfTheFuture = "hello-educative";CompletableFuture<String> stringCompletableFuture = CompletableFuture.completedFuture(resultOfTheFuture);String value = stringCompletableFuture.get();System.out.println("Returned value - " + value);}}
CompletableFuture
and name it resultOfTheFuture
.CompletableFuture
that is in a completed stage using the completedFuture()
method, passing resultOfTheFuture
as the argument to the method.completedFuture()
obtained in Line 8.