Add two maps in Groovy while summing up values for common keys

前端 未结 7 916
花落未央
花落未央 2020-12-16 12:24

I have two maps in Groovy [a: 1, b: 2] and [b:1, c:3] and would like to create from them a third map [a: 1, b: 3, c: 3]. Is there a Gr

7条回答
  •  执笔经年
    2020-12-16 12:55

    def merge(map1, map2) { 
        def add = { map, entry -> map << entry }
        map2.inject(map1.inject([:], add), add)
    }
    

提交回复
热议问题