How can I sum the values in two maps and return the map with summed values using guava ? It can be safely assumed, both maps will have the same set of keys.
For e.g.:
Map<OccupancyType, BigDecimal> filteredPrice
[ 1 : 100 ]
[ 2 : 50 ]
[ 3 : 200 ]
Other map
Map<OccupancyType, BigDecimal> pkgPrice
[ 1 : 10 ]
[ 2 : 20 ]
[ 3 : 30 ]
Summed map
Map<OccupancyType, BigDecimal> sumPrice
[ 1 : 110 ]
[ 2 : 70 ]
[ 3 : 230 ]
I know I can iterate through these maps and sum the values easily, but is there a cleaner way to do this using one of the guava methods ?
Guava contributor here.
If you’re sure that both maps have the same keys, I suppose you could do
but that said, this seems to fall squarely into the category of “the direct approach is the cleanest.” (Additionally, this implementation will recompute the values every time you request them, unless you do a copy. Still, this is almost certainly unnecessarily complicated; the direct, imperative approach is almost certainly preferable here.