IntPipelineError in a nested loop
Jose
jgetino at telefonica.net
Tue Apr 9 03:58:59 PDT 2013
Thanks Paul,
I forgot again that streams get conssumed!! :-(
However there seems to be some more problems with the code, as I still get
an error message like this:
"Exception in thread "main" java.lang.IncompatibleClassChangeError:
Class java.util.stream.IntPipeline does not implement the requested
interface util.logger.functions.IntBiConsumer"
On the other hand I tried your suggestion of using:
flatMap() instead of fold()
but then I get a linear stream, and I can't plug a biconsumer to the output.
The biconsumer needs to be aware of both coordinates x, and y because in
practical cases they have different meanings.
The new code, with flatMap included :
import java.util.function.Supplier;
import java.util.stream.IntStream;
import java.util.stream.Streams;
@FunctionalInterface
public interface IntBiConsumer {
void accept(int n, int m);
static void fold(IntStream sx, Supplier<IntStream> ssy, IntBiConsumer
bc) {
sx.forEach(nx -> ssy.get().forEach(ny -> bc.accept(nx, ny)));
}
class Demo {
public static void main(String[] args) {
Supplier<IntStream> ssx = () -> Streams.intRange(0, 10);
Supplier<IntStream> ssy = () -> Streams.intRange(0, 5);
IntBiConsumer bc = (x, y) -> System.out.println("x: " + x + ",
y: " + y);
/*----------- FLAT ---------*/
IntStream sx=ssx.get();
IntStream flatted= sx.flatMap(n -> ssy.get());
flatted.forEach(n -> System.out.println("n: " + n));
/*----------- FOLD ---------*/
sx=ssx.get();
fold(sx, ssy, bc);
}
}
}
More information about the lambda-dev
mailing list