为什么这段代码可以用 jdk8u45 及更高版本编译,但不能用 jdk8u25 编译? [英] Why this code compiles with jdk8u45 and above but not with jdk8u25?

查看:29
本文介绍了为什么这段代码可以用 jdk8u45 及更高版本编译,但不能用 jdk8u25 编译?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请问,有人能帮我弄清楚为什么下面的代码可以用 jdk8u45 及更高版本编译,但用 jdk8u25 编译失败吗?我查看了 JDK 发行说明,但没有找到与该问题相关的任何内容,或者可能错过了.

Please, could someone help me to figure out why the following code compiles with jdk8u45 and above but fails with jdk8u25? I looked through the JDK release notes but didn't find anything related to the issue or maybe missed it.

代码

public class Main {

    static class Param {
        final int id;

        Param(int id) {
            this.id = id;
        }
    }

    static class Subtask {
        final Param param;

        Subtask(Param param) {
            this.param = param;
        }
    }

    public static void main(String[] args) {
        List<? extends Param> params = IntStream.range(1, 100).mapToObj(Param::new).collect(Collectors.toList());
        NavigableMap<String, Subtask> map = params.stream()
                .collect(Collectors.toMap(p -> UUID.randomUUID().toString(), Subtask::new, (a, b) -> a, TreeMap::new));
    }
}

jdk8u25 异常:

jdk8u25 exception:

Error:(33, 17) java: no suitable method found for collect(java.util.stream.Collector<org.ka.Main.Param,capture#1 of ?,java.util.TreeMap<java.lang.String,org.ka.Main.Subtask>>)
    method java.util.stream.Stream.<R>collect(java.util.function.Supplier<R>,java.util.function.BiConsumer<R,? super capture#2 of ? extends org.ka.Main.Param>,java.util.function.BiConsumer<R,R>) is not applicable
      (cannot infer type-variable(s) R
        (actual and formal argument lists differ in length))
    method java.util.stream.Stream.<R,A>collect(java.util.stream.Collector<? super capture#2 of ? extends org.ka.Main.Param,A,R>) is not applicable
      (cannot infer type-variable(s) R,A,capture#3 of ?,T,K,U,M,K,V
        (argument mismatch; java.util.stream.Collector<capture#2 of ? extends org.ka.Main.Param,capture#4 of ?,java.util.TreeMap<java.lang.Object,org.ka.Main.Subtask>> cannot be converted to java.util.stream.Collector<? super capture#2 of ? extends org.ka.Main.Param,capture#4 of ?,java.util.TreeMap<java.lang.Object,org.ka.Main.Subtask>>))

推荐答案

我在类型推断方面遇到了类似的问题,该问题在 8u5 和 8u25 之间的某个地方中断,并在 8u40 中得到修复.8u40 中的错误修复列表有一些 javac 修复与 嵌套的 lambda 主体错误地排除了一些重载解析中的方法,这就是我认为您的问题所在.

I had similar problems with type inferencing which broke somewhere between 8u5 and 8u25 and was fixed in 8u40. The bug fix list in 8u40 has some javac fixes having to do with nested lambda bodies incorrectly ruling out some methods in overload resolution which is what I think your problem is.

这是8u40 中所有错误修复的列表

这篇关于为什么这段代码可以用 jdk8u45 及更高版本编译,但不能用 jdk8u25 编译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆