面试题答案
一键面试CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> 5)
.thenApply(i -> String.valueOf(i * i));
thenApply
方法的参数类型:Function<? super T,? extends U>
,这里T
为Integer
,Function
接收一个Integer
类型参数并返回一个U
类型(这里U
为String
)。thenApply
方法的返回值类型:CompletableFuture<U>
,这里U
为String
,即返回一个CompletableFuture<String>
。