LearnFrenzy
Sign in Take a free test

Concurrency and Executors · Predict the Output · Question 5

A CompletableFuture thenApply chain Answer, explanation & discussion

  • Output
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 5 Back to Exercise

What is the output of the following Java program?

Main.java
import java.util.concurrent.CompletableFuture;

public class Main {
    public static void main(String[] args) {
        String r = CompletableFuture.supplyAsync(() -> "  learn java  ")
            .thenApply(String::strip)
            .thenApply(String::toUpperCase)
            .thenApply(s -> s.replace(' ', '-'))
            .thenApply(s -> s + "!" + s.length())
            .join();
        System.out.println(r);
    }
}
  • A LEARN-JAVA!10 Correct
  • B LEARN JAVA!10
  • C LEARN-JAVA!14
  • D LEARN-JAVA !14
Correct Answer: Option A

Each thenApply transforms the previous result, like map on a stream.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links