java 8 nested loop with stream

svini :

I have a for loop iterating over a Integer [][]map. Currently is like this:

for(int i = 0; i < rows; i++) {
    for(int j = 0; j < columns; j++) {
        if(map[i][j] == 1)
            q.add(new Point(i,j));
    }
}        

Instead of 2d array, suppose I have List<List<Integer>> maps2d. How would I do that with streams?

So far I got this:

maps2d.stream()
      .forEach(maps1d -> maps1d.stream()
                               .filter(u -> u == 1)
                               .forEach(u -> {

                               }
      )
);

Is it correct so far? If yes, how do I count i and j in order to create the new Point(i,j) and add it to q ?

Slaw :

If you really want to use streams for the same purpose then one option is to use nested IntStreams to iterate over the indices. As an example:

public static List<Point> foo(List<List<Integer>> map) {
  return IntStream.range(0, map.size()) // IntStream
      .mapToObj(
          i ->
              IntStream.range(0, map.get(i).size())
                  .filter(j -> map.get(i).get(j) == 1)
                  .mapToObj(j -> new Point(i, j))) // Stream<Stream<Point>>
      .flatMap(Function.identity()) // Stream<Point>
      .collect(Collectors.toList()); // List<Point>
}

Personally, I don't find that tremendously readable. Note you can still use nested for loops with your list, similar to your current solution:

public static List<Point> foo(List<List<Integer>> map) {
  List<Point> result = new ArrayList<>();
  for (int i = 0; i < map.size(); i++) {
    List<Integer> inner = map.get(i);
    for (int j = 0; j < inner.size(); j++) {
      if (inner.get(j) == 1) {
        result.add(new Point(i, j));
      }
    }
  }
  return result;
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=414739&siteId=1