This code compiles using ecj but not javac. Is this a bug in ecj, javac or neither? : <p>The following code creates a <code>Collector</code> that produces an <code>UnmodifiableSortedSet</code>:</p>

<pre><code>package com.stackoverflow;

import java.util.Collections;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.stream.Collector;
import java.util.stream.Collectors;

public class SOExample {

    public static &lt;T extends Comparable&lt;T&gt;&gt; Collector&lt;T, ?, SortedSet&lt;T&gt;&gt; toSortedSet() {
        return Collectors.toCollection(TreeSet::new);
    }

    public static &lt;T extends Comparable&lt;T&gt;&gt; Collector&lt;T, ?, SortedSet&lt;T&gt;&gt; toUnmodifiableSortedSet() {
        return Collectors.collectingAndThen(toSortedSet(), Collections::&lt;T&gt; unmodifiableSortedSet);
    }
}
</code></pre>

<p>The codes compiles under the ecj compiler:</p>

<pre><code>$ java -jar ~/Downloads/ecj-3.13.101.jar -source 1.8 -target 1.8 SOExample.java
Picked up _JAVA_OPTIONS: -Duser.language=en -Duser.country=GB
</code></pre>

<p>Under javac however:</p>

<pre><code>$ javac -version
Picked up _JAVA_OPTIONS: -Duser.language=en -Duser.country=GB
javac 1.8.0_73

$ javac SOExample.java
Picked up _JAVA_OPTIONS: -Duser.language=en -Duser.country=GB
SOExample.java:16: error: method collectingAndThen in class Collectors cannot be applied to given types;
        return Collectors.collectingAndThen(toSortedSet(), Collections::&lt;T&gt; unmodifiableSortedSet);
                         ^
  required: Collector&lt;T#1,A,R&gt;,Function&lt;R,RR&gt;
  found: Collector&lt;T#2,CAP#1,SortedSet&lt;T#2&gt;&gt;,Collection[...]edSet
  reason: cannot infer type-variable(s) T#3
    (actual and formal argument lists differ in length)
  where T#1,A,R,RR,T#2,T#3 are type-variables:
    T#1 extends Object declared in method &lt;T#1,A,R,RR&gt;collectingAndThen(Collector&lt;T#1,A,R&gt;,Function&lt;R,RR&gt;)
    A extends Object declared in method &lt;T#1,A,R,RR&gt;collectingAndThen(Collector&lt;T#1,A,R&gt;,Function&lt;R,RR&gt;)
    R extends Object declared in method &lt;T#1,A,R,RR&gt;collectingAndThen(Collector&lt;T#1,A,R&gt;,Function&lt;R,RR&gt;)
    RR extends Object declared in method &lt;T#1,A,R,RR&gt;collectingAndThen(Collector&lt;T#1,A,R&gt;,Function&lt;R,RR&gt;)
    T#2 extends Comparable&lt;T#2&gt;
    T#3 extends Object declared in method &lt;T#3&gt;unmodifiableSortedSet(SortedSet&lt;T#3&gt;)
  where CAP#1 is a fresh type-variable:
    CAP#1 extends Object from capture of ?
1 error
</code></pre>

<p>If I change the offending line to the following, the code compiles under both compilers:</p>

<pre><code>return Collectors.collectingAndThen(toSortedSet(), (SortedSet&lt;T&gt; p) -&gt; Collections.unmodifiableSortedSet(p));
</code></pre>

<p>Is this a bug in ecj, javac or an underspecification that allows for both behaviours?</p>

<p>Javac behaves the same in java 9 and 10.</p>
