我在文件 Sandbox.java 中有以下类:
package sandbox;
import java.util.Arrays;
import java.util.Collection;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;
public class Sandbox {
public static void main(String[] args) throws Exception {
ExecutorService executor = Executors.newSingleThreadExecutor();
Collection<String> s = Arrays.asList(1,2,4,100).stream()
.map(i -> CompletableFuture
.supplyAsync(() -> Wrapper.of(i), executor)
.thenApply(d -> d.get().toString())
)
.map(CompletableFuture::join)
.collect(Collectors.toList());
executor.shutdown();
System.out.println(s);
}
}
class Wrapper<T> {
T t;
private Wrapper(T t) {
this.t = t;
}
public T get() {
return t;
}
public static <T> Wrapper<T> of (T t) {
return new Wrapper<>(t);
}
}
Eclipse 中的编译在第 14 行显示错误“无法推断 map(Function) 的类型参数”。
使用纯 javac (JDK 1.8.0_121) 可以毫无问题地编译相同的代码。
如果我将正确的行更改为:
Collection<String> s = Arrays.asList(1,2,4,100).stream()
.map(i -> CompletableFuture
.supplyAsync(() -> Wrapper.of(i), executor)
.<String>thenApply(d -> d.get().toString())
)
.map(CompletableFuture::join)
.collect(Collectors.toList());
然后代码在 Eclipse 中编译没有错误。
有谁知道为什么会有这样的行为?是错误吗?
我使用 Eclipse 4.6.2.20161208-0625(目前没有发现更新)。
最佳答案
我已经确认,这是一个错误:https://bugs.eclipse.org/bugs/show_bug.cgi?id=512486 .它已在 4.6.3 中声明为已解决。当稳定版本可用时,我会确认这一点。
关于java - 在某些特定情况下,无法为 <R> map(Function<? super T,? extends R>) 推断类型参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42285370/