我正在使用 Groovy 2.1.0 版。我正在尝试从地图中读取值并更新另一个地图。
a = ["key1":"" ,"key2":""]
b = ["key1": "10", "key3":"99" ]
for (i in b){
if( i.key in a)
a.(i.key) = i.value
}
def key2 = "key2"
a.key2 = "value2"
println a.get("key2")
println "value returned is :" + a.get("key1") + ":"
这导致 o/p
value2
value returned is ::
但是,如果地图“a”不包含空字符串作为值,那么它可以按预期工作。
a = ["key1":"7" ,"key2":""]
b = ["key1": "10", "key3":"99" ]
for (i in b){
if( i.key in a)
a.(i.key) = i.value
}
a."key2" = "value2"
println a.get("key2")
println "value returned is :" + a.get("key1") + ":"
这导致 o/p
value2
value returned is :10:
我想用值更新空地图,就像在第一个场景中一样。我在哪里弄错了。
提前致谢。