I have a map of objects that look like this:
KEY[string]: id
VALUE[object]: Stock
Stock object has a price
attribute that is a BigDecimal.
I want to iterate through this map and find a) the sum b) max stock price.
I want to use Java 8 streams API to increase performance so I tried the following:
AtomicReference<BigDecimal> sum = new AtomicReference<>(new BigDecimal(0));
myMap.forEach( (id, Stock) -> {
sum.updateAndGet(v -> v.add(Stock.getPrice()));
})
Is this the correct way to total BigDecimal using forEach? And how can I do the same to find the max stock price in the map?