I have a: List<Map<String, Long>> items = new ArrayList<>();
I would like to get a Map in which the key is grouped by, and the value is the sum.
Example: List
- Item 0
- foo -> 1
- bar -> 2
- Item 1
- foo -> 4
- bar -> 3
Result: Map
- foo -> 5
- bar -> 5
I know how to do this the "long" way, but was trying to discover a lambda/streaming/groupby approach using the java 8 features. any thoughts?
You can use
groupingBy
collector:Or you can use
toMap
: