Q:

A problem with IntStream in int[][], I don't know what allMatch() doing

After becoming famous, the CodeBots decided to move into a new building together. Each of the rooms has a different cost, and some of them are free, but there's a rumour that all the free rooms are haunted! Since the CodeBots are quite superstitious, they refuse to stay in any of the free rooms, or any of the rooms below any of the free rooms.
Given a matrix, a rectangular matrix of integers, where each value represents the cost of the room, your task is to return the total sum of all rooms that are suitable for the CodeBots (ie: add up all the values that don't appear below a 0).
Example:
for:
matrix = [[0, 1, 1, 2], 
          [0, 5, 0, 0], 
          [2, 0, 3, 3]]

the output should be
matrixElementsSum(matrix) = 9

Can anyone explain how allMatch works in this code?
int matrixElementsSum(int[][] matrix) {
        return IntStream.range(0, matrix.length)
                .map(i -> IntStream.range(0, matrix[0].length)
                        .map(j -> IntStream.range(0, i)
                                .allMatch(k -> matrix[k][j] > 0)
                                ? matrix[i][j] : 0)
                        .sum())
                .sum();
}

A:

allMatch() method takes an IntPredicate object. IntPredicate is a Functional Interface whose "functional method" is test(int). Your code passes all the elements to it and checks if all of them match >0. You could also use anyMatch() method here. Then your code will be like this:
int matrixElementsSum(int[][] matrix) {
    return IntStream.range(0, matrix.length)
            .map(i -> IntStream.range(0, matrix[0].length)
                    .map(j -> IntStream.range(0, i)
                            .anyMatch(k -> matrix[k][j] == 0)
                            ? 0 : matrix[i][j] )
                    .sum())
            .sum();
}

Also, the time complexity of this algorithm is f(i,j) = j.(i(i+1)/2) = O(j.i^2) where i is the number of rows and j is the number of columns.

