Sobes.tech
Junior — Middle

How can you extract the string value from a Future object?

sobes.tech AI

Answer from AI

In Java, the object Future<String> represents the result of an asynchronous operation that can be obtained in the future. To extract the string value from such an object, you need to call the get() method. This method blocks the current thread until the result becomes available.

Example:

Future<String> future = executorService.submit(() -> "Hello, World!");
try {
    String result = future.get(); // blocks until the result is ready
    System.out.println(result);
} catch (InterruptedException | ExecutionException e) {
    e.printStackTrace();
}

If you want to get the result without blocking, you can use future.isDone() to check if the result is ready before calling get().

How can you extract the string value from a Future… - sobes.tech