Java 8 CompletableFuture.thenApply()
Java 8 CompletableFuture.thenApply()
终于在 Java 8 中使用 CompletableFuture dealio。我遇到了一个我不太明白的编译错误(在我的 IDE 中)。
我有 List<String>
个标识符,我想将其附加到 URL,然后异步调用每个 url。到目前为止,我只有这两种方法。
private void process(List<String> identifiers) {
List<CompletableFuture<String>> futures = identifiers.stream()
.map(CompletableFuture.thenApply(this::sendRequest))
.collect(toList());
}
private void sendRequest(String s) {
// do some URL building and append the string to the end of the url.
// then call it, don't care about result yet
}
我遇到的编译器错误出现在第一种方法的 this::sendRequest
部分。它抱怨我的 class 没有定义 sendRequest(Object)
方法。
但我认为通过输入 identifiers
我不需要担心在我的 lambda 表示法中调用类型?我什至不确定如何使用 ::
运算符指定类型。也许我什至不应该使用 ::
运算符?我很困惑。
thenApply
必须在已存在的 CompletableFuture
对象上调用。例如,
List<CompletableFuture<String>> futures = identifiers.stream()
.map(CompletableFuture::completedFuture) // makes CompletableFuture<String>
.map(f -> f.thenApply(this::sendRequest))
.collect(toList());
终于在 Java 8 中使用 CompletableFuture dealio。我遇到了一个我不太明白的编译错误(在我的 IDE 中)。
我有 List<String>
个标识符,我想将其附加到 URL,然后异步调用每个 url。到目前为止,我只有这两种方法。
private void process(List<String> identifiers) {
List<CompletableFuture<String>> futures = identifiers.stream()
.map(CompletableFuture.thenApply(this::sendRequest))
.collect(toList());
}
private void sendRequest(String s) {
// do some URL building and append the string to the end of the url.
// then call it, don't care about result yet
}
我遇到的编译器错误出现在第一种方法的 this::sendRequest
部分。它抱怨我的 class 没有定义 sendRequest(Object)
方法。
但我认为通过输入 identifiers
我不需要担心在我的 lambda 表示法中调用类型?我什至不确定如何使用 ::
运算符指定类型。也许我什至不应该使用 ::
运算符?我很困惑。
thenApply
必须在已存在的 CompletableFuture
对象上调用。例如,
List<CompletableFuture<String>> futures = identifiers.stream()
.map(CompletableFuture::completedFuture) // makes CompletableFuture<String>
.map(f -> f.thenApply(this::sendRequest))
.collect(toList());