gpt4 book ai didi

collections - 将两张 map 合并到 Groovy 中的结果 map

转载 作者:行者123 更新时间:2023-12-04 22:29:40 28 4
gpt4 key购买 nike

我有两张 map :

firstMap = [totalProjectCount:53, activeProjectCount:29, completedProjectCount:1, userCount:85]
secondMap = [totalProjectCount:48, activeProjectCount:41, completedProjectCount:0, userCount:123]

我需要结果,例如:
resultMap = [totalProjectCount:101, activeProjectCount:70, completedProjectCount:1, userCount:208]

最佳答案

Map additionJoin( Map firstMap, Map secondMap )
{
def resultMap = [:];
resultMap.putAll( firstMap );
resultMap.putAll( secondMap );

resultMap.each { key, value ->
if( firstMap[key] && secondMap[key] )
{
resultMap[key] = firstMap[key] + secondMap[key]
}
}

return resultMap;
}

def firstMap = [totalProjectCount:53, activeProjectCount:29, completedProjectCount:1, userCount:85]
def secondMap = [totalProjectCount:48, activeProjectCount:41, completedProjectCount:0, userCount:123]


def resultMap = additionJoin( firstMap , secondMap )

println resultMap

关于collections - 将两张 map 合并到 Groovy 中的结果 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34326587/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com