The following statement, although nonsensical, appears syntactically sound.
final Stream<LongStream> foobar = IntStream.empty()
.flatMap(x -> IntStream.empty()
.mapToObj(y -> IntStream.empty()
.mapToLong(z -> 1))); //compilation error here on `z -> 1`
However it does not compile, returning:
java: incompatible types: bad return type in lambda expression no instance(s) of type variable(s) U exist so that java.util.stream.Stream conforms to java.util.stream.IntStream
However if you delay the flatmap, everything works fine:
final Stream<LongStream> foobar = IntStream.empty()
.mapToObj(x -> IntStream.empty()
.mapToObj(y -> IntStream.empty()
.mapToLong(z -> 1)))
.flatMap(x -> x);
What is the difference between .mapToObj(..).flatMap(..)
and just .flatMap(..)
? Is there someway to eliminate the extra flatmap call?
.mapToObj(..).flatMap(..)
and .flatMap(..)
expect completely different signatures.
.mapToObj(..).flatMap(..)
expects an int -> Object
function, and an Object -> Stream<?>
function.
.flatMap(..)
expects an int -> IntStream
function.
If you break down your code, you're passing an int -> Stream<LongStream>
function, which isn't compatible with an int -> IntStream
function.
You would have the same error with this simplified code:
IntStream.empty().flatMap(x -> Stream.of(LongStream.empty()));
I've refactored your method to break down what it's doing:
IntFunction<LongStream> f1 = y -> IntStream.empty().mapToLong(z -> 1);
IntFunction<LongStream> f2 = x -> IntStream.empty().mapToObj(f1);
final Stream<LongStream> foobar = IntStream.empty().flatMap(f2);
We have two things wrong here:
The lambda on line 2 does not return a LongStream
, but rather a Stream<LongStream>
, as we are converting each int
in our stream to a LongStream
. If you intend for it to be a single LongStream
, you need to do a flatMapToLong
.
The flatMap
on line 3 expects an int -> int
function, which yours is not. However, you can use mapToObj
instead, which takes the method that you're providing it.
So the corrected method would be:
IntFunction<LongStream> f1 = y -> IntStream.empty().mapToLong(z -> 1);
IntFunction<LongStream> f2 = x -> IntStream.empty().mapToObj(f1).flatMapToLong(i -> i);
final Stream<LongStream> foobar = IntStream.empty().mapToObj(f2);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With