I have a map which contains a map. Map> For all entries in the map, I want to calculate the sum of a particular key.
For example my map is something like this:
Key1 Key2 Value
A Z 10.10
B Z 40.10
C Y 20.10
I want to calculate basically the sum of all the key2 which is equal to B. So in this case I want to get 50.20 as Key1 -C does not have key2 Z
I am trying to do this using Java 8. I am not sure how I should collect the sum.
double sum = 0;
myMap.forEach((key1, key2) -> {
sum += key2.get("Z");
});
But then I get an error saying that value inside lambda should be a final.