在某些特定情况下,无法推断<R> map(Function<? super T,? 扩展 R>) 的类型参数
2022-09-02 22:14:00
						我在文件沙盒中有以下类.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(目前它没有找到任何更新)。