I am transforming guava Table object, applying weights.
method input Type is below, where Row and Column are some classes.
columnWeightsPerRow : Table<Row, Column, Double>
weightsPerRow : Map<Row, Double>
Here are 2 ways I have written so far, can you suggest better way which is more readable ?
This is first way I am writing code.
Table<Row, Column, Double> applyWeights(Table<Row, Column, Double> columnWeightsPerRow,
Map<Row, Double> weightsPerRow)
{
Table<Row, Column, Double> rowWeightedColumnWeights =
HashBasedTable.create();
columnWeightsPerRow
.rowMap()
.entrySet()
.stream()
.forEach( columnWeightsPerRowEntry -> columnWeightsPerRowEntry
.getValue().entrySet()
.forEach(weightPerColumn ->
rowWeightedColumnWeights.put(columnWeightsPerRowEntry.getKey(),
weightPerColumn.getKey(),
weightPerColumn.getValue() * weightsPerRow.get(columnWeightsPerRowEntry.getKey()))));
return rowWeightedColumnWeights;
}
This is second way of writing same code.
Table<Row, Column, Double> applyWeights(Table<Row, Column, Double> columnWeightsPerRow,
Map<Row, Double> weightsPerRow)
{
return columnWeightsPerRow
.rowMap()
.entrySet()
.stream()
.collect(HashBasedTable::create,
(table, entry) ->
entry.getValue()
.entrySet()
.stream()
.forEach(columnWeightEntry -> table.put(entry.getKey(),
columnWeightEntry.getKey(),
columnWeightEntry.getValue() * weightsPerRow.get(entry.getKey()))),
HashBasedTable::putAll);
}