I have the following piece of code
public class TeeingCollector {
public static void main(String[] args) {
// var strs = List.of("abc");
var dividedStrings = Stream.of("foo", "hello", "bar", "world")
.collect(Collectors.teeing(
Collectors.filtering((String s) -> s.length() <= 3, Collectors.toList()),
Collectors.filtering((String s) -> s.length() > 3, Collectors.toList()),
List::of
));
System.out.println(dividedStrings);
}
private static class Employee {
boolean isActive;
public Employee(boolean isActive) {
this.isActive = isActive;
}
public boolean isActive() {
return isActive;
}
@Override
public String toString() {
return "Employee{" +
"isActive=" + isActive +
'}';
}
}
private static class MaxMin {
int max;
int min;
MaxMin(int max, int min) {
this.max = max;
this.min = min;
}
@Override
public String toString() {
return "MaxMin{" +
"max=" + max +
", min=" + min +
'}';
}
}
}
If I execute that class from terminal with java src/TeeingCollector.java
I get the following error:
src/TeeingCollector.java:14: error: incompatible types: inferred type does not conform to equality constraint(s)
.collect(Collectors.teeing(
^
inferred: List<String>
equality constraints(s): List<Object>,R
where R,T,A are type-variables:
R extends Object declared in method <T,A,R>filtering(Predicate<? super T>,Collector<? super T,A,R>)
T extends Object declared in method <T,A,R>filtering(Predicate<? super T>,Collector<? super T,A,R>)
A extends Object declared in method <T,A,R>filtering(Predicate<? super T>,Collector<? super T,A,R>)
1 error
error: compilation failed
If I uncomment the line var strs = List.of("abc");
then the code is executed without any problem.
Java version (for macOS):
OpenJDK Runtime Environment (build 12+33)
OpenJDK 64-Bit Server VM (build 12+33, mixed mode, sharing)
running the same code with the following version (older) yields no errors
OpenJDK Runtime Environment (build 12-ea+23)
OpenJDK 64-Bit Server VM (build 12-ea+23, mixed mode, sharing)
Note: if I compile it then run, I don't have any errors with both builds for macOS, so it seems that only java TeeingCollector.java
isn't working properly
Employee
andMaxMin
classes relevant? – JeepEmployee
andMaxMin
are unused, but if I remove them then I can't reproduce the error – Wartbuild 12+33
, but I don't understand how uncommentingvar strs = List.of("abc");
make it compile in your case; or that you can compile it all : usingjavac
fails too. Are you sure about the example you provided here? – Drolljava
, not compiling first explicitly viajavac
... I can confirm to reproduce this on mac. so darn weird... sounds like a bug that should be opened. it's even weirder thatjavac --release 12
will compile that, but dropping--release
will not... – Drollbrew cask
!= from the most recent one... I downloaded the latest one available and it works just fine, but at the same time it's not available in the homebrew repos yet - I tried to upgrade and it's not there yet. – Droll