I'm planning to leverage the benefits of using parallel streams in my program and I want to replace a loop like this:
for (int gridY = gridYFrom; gridY <= gridYTo; gridY++) {
for (int gridX = gridXFrom; gridX <= gridXTo; gridX++) {
coordinates.add(Coordinate.from(gridX, gridY));
}
}
with something like this:
IntStream.rangeClosed(gridYFrom, gridYTo).parallel().map(y ->
IntStream.rangeClosed(gridXFrom, gridXTo).mapToObj(x -> {
return Coordinate.from(x, y);
}
)).collect(Collectors.toSet());
My problem is that here I get a cyclic inference error. I understand that I am supposed to return an int from the inner map to be compatible with the outer one but I want to return a Coordinate object (thus the mapToObj call). Is it possible to do so using collect and without using a forEach construct?