Counting specific characters in two dimensional array using stream

Viewed 1555

I would like to count occurrences of a character (for example the space: ' ' ) in 2D Array, using stream. I was trying to find a solution. Here is my code, using a nested loops:

public int countFreeSpaces() {
    int freeSpaces = 0;
    for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++) {
            if (board[j][i] == ' ') freeSpaces++;
        }
    }
    return freeSpaces;
}
2 Answers
Related