21

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.

The code

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 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>>))
Dale
  • 1,613
  • 4
  • 22
  • 42
Alex K.
  • 714
  • 4
  • 14
  • 3
    Probably a bug in 8u25. The error doesn't make a whole lot of sense to me. (I think the argument mismatch is wrong and it also inferred `TreeMap` for some reason.) – Radiodef May 21 '16 at 21:40
  • Is it about Oracle JDK? – Andremoniy May 21 '16 at 21:44
  • 1
    I cannot find any bug report for this, but I don't see why this shouldn't compile. It was most likely a bug. – Tunaki May 22 '16 at 00:35
  • 4
    I've seen this problem before. I think it was resolved in 8u40. If it's reopened I can give a proper answer but I think the bug in javac was this : http://bugs.java.com/view_bug.do?bug_id=8033483 – dkatzel May 22 '16 at 03:13
  • @dkatzel looks like it's the answer. If you post it I will accept it as the answer. Thank you. – Alex K. May 22 '16 at 11:10

1 Answers1

15

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.

Here is the list of all bug fixes in 8u40

dkatzel
  • 31,188
  • 3
  • 63
  • 67