Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator
completablefuture

What is CompletableFuture.toCompletableFuture() in Java?

abhilash

Overview

toCompletableFuture() is an instance method of the CompletableFuture class. It is used to return the same completable future upon which this method is invoked.

The toCompletableFuture() 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;
Import statement

Syntax

public CompletableFuture<T> toCompletableFuture()
Syntax

Parameters

The method has no parameters.

Return value

This method returns the same Future on which the method is invoked.

Code

import java.util.concurrent.*;

public class Main {

    public static void main(String[] args) {
        CompletableFuture<String> completableFuture = new CompletableFuture<>();
        System.out.println(completableFuture.equals(completableFuture.toCompletableFuture()));
    }
}
Implementation of the toCompletableFuture() method

Explanation

  • Line 1: We import the relevant packages.
  • Line 6: We create a completable future.
  • Line 7: We check whether the Future created in line 6 and the Future returned by invoking toCompletableFuture() method are equal using the equals() method.

RELATED TAGS

java
communitycreator
completablefuture
RELATED COURSES

View all Courses

Keep Exploring