I have 2 list of lists as below:
List<List<Integer>> val1 : [[2, 3, 0], [1, 3, 5], [0, 2, 4]]
List<List<Integer>> val2 : [[0, 3, 1], [3, 1, 4], [1, 1, 2]]
I wish to compute a new list of lists (sum) that has the sum of corresponding index from the two lists above.
List<List<Integer>> sum : [[2, 6, 1], [4, 4, 9], [1, 3, 6]]
Is there a better way of doing this using Java 8? Currently I'm using a for loop to achieve this and it doesn't look nice.
for (int rowIdx = 0; rowIdx < val1.size(); rowIdx++) {
for (int colIdx = 0; colIdx < val2.get(0).size(); colIdx++) {
final int cumSum = val1.get(rowIdx).get(colIdx) + val2.get(rowIdx).get(colIdx);
sum.get(rowIdx).set(colIdx, cumSum);
}
}