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

前端 未结 7 915
花落未央
花落未央 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)
    }
    
    0 讨论(0)
提交回复
热议问题