The following code creates a Collector
that produces an UnmodifiableSortedSet
:
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 <T extends Comparable<T>> Collector<T, ?, SortedSet<T>> toSortedSet() {
return Collectors.toCollection(TreeSet::new);
}
public static <T extends Comparable<T>> Collector<T, ?, SortedSet<T>> toUnmodifiableSortedSet() {
return Collectors.collectingAndThen(toSortedSet(), Collections::<T> unmodifiableSortedSet);
}
}
The codes compiles under the ecj compiler:
$ 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
Under javac however:
$ 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::<T> unmodifiableSortedSet);
^
required: Collector<T#1,A,R>,Function<R,RR>
found: Collector<T#2,CAP#1,SortedSet<T#2>>,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 <T#1,A,R,RR>collectingAndThen(Collector<T#1,A,R>,Function<R,RR>)
A extends Object declared in method <T#1,A,R,RR>collectingAndThen(Collector<T#1,A,R>,Function<R,RR>)
R extends Object declared in method <T#1,A,R,RR>collectingAndThen(Collector<T#1,A,R>,Function<R,RR>)
RR extends Object declared in method <T#1,A,R,RR>collectingAndThen(Collector<T#1,A,R>,Function<R,RR>)
T#2 extends Comparable<T#2>
T#3 extends Object declared in method <T#3>unmodifiableSortedSet(SortedSet<T#3>)
where CAP#1 is a fresh type-variable:
CAP#1 extends Object from capture of ?
1 error
If I change the offending line to the following, the code compiles under both compilers:
return Collectors.collectingAndThen(toSortedSet(), (SortedSet<T> p) -> Collections.unmodifiableSortedSet(p));
Is this a bug in ecj, javac or an underspecification that allows for both behaviours?
Javac behaves the same in java 9 and 10.
toSortedSet
as well:return Collectors.collectingAndThen(Collectors.toCollection(TreeSet::new), Collections::<T>unmodifiableSortedSet);
– Calton