groovy -v
:Groovy Version: 1.8.6 JVM: 1.6.0_26 Vendor: Sun Microsystems Inc. OS: Linux
:
def map = new HashMap()
def keyVariable = "a"
def valueVariable = "b"
map.put("${keyVariable}", valueVariable)
for (String key : map.keySet()) {
println map.get(key)
}
这会打印null
。有人可以解释为什么Groovy找到密钥,但是找不到相同密钥的相应值吗?
答案 0 :(得分:0)
问题中的代码不会为我打印null
(Groovy 2.1.6)
更多惯用的Groovy将是:
def map = [:]
def keyVariable = "a"
def valueVariable = "b"
map[ keyVariable ] = valueVariable
map.each { key, value ->
println value
}
// Or
map.keySet().each { key ->
println map[ key ]
}