Java IntelliJ成功推断类型,但编译会导致推断错误

Java IntelliJ成功推断类型,但编译会导致推断错误,java,intellij-idea,java-8,java-stream,type-inference,Java,Intellij Idea,Java 8,Java Stream,Type Inference,我最近遇到了一个有趣的问题,创建了一个从流到地图的收集器,类似于Guava的地图.uniqueIndex()方法。这是我原始方法的实现和签名 public static <T, K, M extends Map<K, T>> Collector<T, ?, M> toIdentityMap(Supplier<M> supplier, Function<? super T, ? extends K> keyMapper) { re

我最近遇到了一个有趣的问题,创建了一个从
地图
的收集器,类似于Guava的
地图.uniqueIndex()
方法。这是我原始方法的实现和签名

public static <T, K, M extends Map<K, T>> Collector<T, ?, M> toIdentityMap(Supplier<M> supplier, Function<? super T, ? extends K> keyMapper) {
    return Collectors.toMap(keyMapper, Function.identity(), throwingMerger(), supplier);
}

所以这个签名应该能够推断T,原因是当
Function.identity()
被强制转换为
Function时,您的第一个变体使用
Function.identity()
对我来说很好;我可以用
javac
的所有版本成功地编译它,我可以找到。除此之外,不需要使用通配符类型,因为目标类型具有这样一个加宽的签名。使用
Function.identity()。因此,使用
ContainingClass.identityCopy()
也可以,但是这个助手方法是完全没有必要的。有趣的是,当IntelliJ将它显示为错误时,我从未尝试编译它,但它确实编译了。。。
Map<Integer, String> map = Stream.of("1", "2").stream().collect(toIdentityMap(HashMap::new, Integer::valueOf));
private static <T> Function<? super T, ? extends T> identityCopy() {
    return Function.identity();
}
public static <T, K, U, M extends Map<K, U>> Collector<T, ?, M> toMap(Function<? super T, ? extends K> keyMapper,
                            Function<? super T, ? extends U> valueMapper,
                            BinaryOperator<U> mergeFunction,
                            Supplier<M> mapSupplier)