为什么这段代码用jdk8u45及以上编译但不用jdk8u25编译?

时间:2016-05-21 21:10:56

标签: java java-8 javac

拜托,有人可以帮我弄清楚为什么下面的代码用jdk8u45及以上代码编译但是jdk8u25失败了吗? 我查看了JDK发行说明,但没有发现任何与此问题相关的内容或者可能错过了它。

代码

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例外:

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>>))

1 个答案:

答案 0 :(得分:13)

我在类型推断方面遇到了类似问题,它在8u5和8u25之间突破,并在8u40中修复。 8u40中的错误修复列表有一些javac修复与 nested lambda bodies incorrectly ruling out some methods in overload resolution有关,这是我认为你的问题。

以下是all bug fixes in 8u40

的列表