0

我有这段代码:

CompletableFuture
    .supplyAsync(() -> {
        return  smsService.sendSMS(number);
    }).thenApply(result -> {
        LOG.info("SMS sended " + result);
    });

但我得到一个编译错误:

thenApply(Function<? super Boolean,? extends U>)类型 中的方法CompletableFuture<Boolean>不适用于参数((<no type> result) -> {})

4

1 回答 1

3

你想用thenAcceptthenApply

thenApply采用Function以下形式

public interface Function<T, R> {
    R apply(T t);
}

thenAccept采用Consumer以下形式

public interface Consumer<T> {
    void accept(T t);
}

您提供的 lambda 没有返回值;它是无效的。因为泛型类型参数不能为 void,所以您的 lambda 不能转换为Function接口。另一方面,Consumerlambda 可以满足的返回类型为 void。

于 2018-10-23T16:25:26.727 回答