Convert classic nested loops with multiple if/else with Java8 Streams

UserIkhar :

I need some guidance on what can be done here using Java 8 Streams. I have the 2D Arrray of test[][] object which holds row, col and value inside. in order to produce different representation, I loop through and manually append different characters to do that.

public class Point{ // resembling a pixel in a file 
   protected int row;
   protected int column;
   protected int val;
   protected Point(int row, int column, int val) {
   ....
}

public class Test{
    private Point[][] test;   // like a rectangle
    ...
}

public String someFunction() {
    StringBuilder result = new StringBuilder(150);
    for (int row = 0; r < 10; row++) {
        for (int col = 0; c < 15; col++) {
            if (test[row][col].getVal() == 1) {
                result.append('L');
            } else if (test[row][col].getVal() == 2) {
                result.append('M');
            } else if (test[row][col].getVal() == 10) {
                result.append('N');
            } else {
                result.append(' ');
            }
        }
        result.append('\n');
    }
    return result.toString();
 }

Also, Generally how does the streams work with 2 loops, some good examples would be great help.

azro :

To simplify a bit this code, you can use for-each loops, this way you won't need to handle indexes

public String someFunction() {
    for (C[] row : test) {
        for (C item : row) {
            if (item.getVal() == 1) {
                result.append('L');
            } else if (item.getVal() == 2) {
                result.append('M');
            } else if (item.getVal() == 10) {
                result.append('N');
            } else {
                result.append(' ');
            }
        }
        result.append('\n');
    }
    return result.toString();
}

You won't win something by using the Stream version because you're doing easy thing just iterating an array so the classic loop is nice, but here it is

public static String someFunction() {
    return Arrays.stream(test)
            .map(row -> Arrays.stream(row).map(item -> {
                switch (item.getVal()) {
                    case 1:
                        return "L";
                    case 2:
                        return "M";
                    case 10:
                        return "N";
                    default:
                        return " ";
                }
            }).collect(Collectors.joining()))
            .collect(Collectors.joining("\n"));
}

And if you want to see it, the Stream version with the indices

public String someFunction() {
    return IntStream.range(0, test.length).mapToObj(rowIdx -> IntStream.range(0, test[rowIdx].length).mapToObj(colIdx -> {
        switch (test[rowIdx][colIdx].getVal()) {
                    ...
        }
    }).collect(Collectors.joining())).collect(Collectors.joining("\n"));
}

Guess you like

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