To process items only, use flatMap
as in Rohit’s answer.
To process elements with their indices, you can use IntStream.range
as follows.
import Java.util.stream.IntStream;
import static Java.util.stream.IntStream.range;
public class StackOverflowTest {
public static void main(String... args) {
int[][] arr = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
// Map the two dimensional array with indices.
final IntStream intStream = range(0, arr.length).flatMap(row -> range(0, arr[row].length).map(col -> {
final int element = arr[row][col];
// E.g. multiply elements in odd numbered rows and columns by two.
return row % 2 == 1 || col % 2 == 1 ? element * 2 : element;
}));
// Prints "1 4 3 8 10 12 7 16 9 ".
intStream.forEachOrdered(n -> System.out.print(n + " "));
}
}