how to merge more than one hashmaps also sum the values of same key in java

前端 未结 8 1573
清歌不尽
清歌不尽 2021-02-13 03:03

ı am trying to merge more than one hashmaps also sum the values of same key, ı want to explain my problem with toy example as follows

    HashMap

        
8条回答
  •  春和景丽
    2021-02-13 03:30

    If you are using Java 8, you can use the new merge method of Map.

    m2.forEach((k, v) -> m.merge(k, v, (v1, v2) -> v1 + v2));
    

提交回复
热议问题