Can someone explain this to me, why does not this expression type get automatically inferred by the compiler ?
Stream.empty().collect(Collectors.summingInt(CharSequence::length))
We know that CharSequence::length
in this context is a ToIntFunction<CharSequence>
, so the type being consumed from the stream elements is a CharSequence
, therefore, the stream being collected is of type Stream<CharSequence>
.
On my IDE, I get the following error :
The method collect(Collector<? super Object,A,R>) in the type Stream is not applicable for the arguments (Collector<CharSequence,capture#1-of ?,Integer>)
As you can see from the error message, the Stream.empty()
gets auto inferred to a Stream<Object>
which is definitely not what I want !