Groovy 中 Map 的奇怪行为 [英] Odd behaviour with Map in Groovy
问题描述
我在编写代码时注意到 Groovy 中处理 XML 和 Maps 时的一些奇怪行为.我想了想,不明白为什么会这样,应该那样做.
I was writing code and I noticed some odd behavior in Groovy when I am dealing with XML and Maps. I thought about it and can't figure out why is it happening and should it that way.
我用 3 个示例编写了示例代码.map1 & 之间的关键区别map3 仅在以下部分:
I wrote sample code with 3 examples. Crucial difference between map1 & map3 is only on the following part:
地图1:
map1 << ["${it.name()}":it.value()]
地图3:
map3["${it.name()}"]=it.value()
这是完整的代码,您可以将其复制粘贴到 Groovy 控制台中:
Here is full code, you can copy-paste it into Groovy console:
def xml = '<xml><head>headHere</head><body>bodyHere</body></xml>'
Map map1 = [:]
def node = new XmlParser().parseText(xml)
node.each {
map1 << ["${it.name()}": it.value()]
}
println map1
println map1["head"]
println ">>>>>>>>>>>>>>>>>>>>>>"
Map map2 = [:]
map2 << ["head":"headHere"]
map2 << ["body":"bodyHere"]
println map2
println map2["head"]
println "<<<<<<<<<<<<<<<<<<<<<<"
def xml2 = '<xml><head>headHere</head><body>bodyHere</body></xml>'
Map map3 = [:]
def node2 = new XmlParser().parseText(xml2)
node2.each {
map3["${it.name()}"]=it.value()
}
println map3
println map3["head"]
我得到的结果如下:
[head:[headHere], body:[bodyHere]]
null
[head:headHere, body:bodyHere]
headHere
[head:[headHere], body:[bodyHere]]
[headHere]
即使你 map1 和 map3 看起来一样,map["head"] 的结果是完全不同的,第一个给出 null,第二个给出实际结果.我不明白为什么会这样.我花了一些时间,但仍然不明白.我使用 .getProperty()
来获取类的信息,但它在地图和内部对象上看起来和感觉都一样.我尝试了更多的东西,但没有任何东西让我知道发生了什么.我什至尝试了不同的操作系统(Win XP、Mac OS),但仍然没有.
Even thou map1 and map3 look the same, the result of map["head"] is totally different, first gives null and second gives the actual result. I don't understand why is it happening. I spent some time on it and still don't get it. I used .getProperty()
to get info on a class, but it looks the same and feels the same on both maps and object inside. I tried couple more things and nothing gives me any idea on what is happening. I even tried different OS (Win XP, Mac OS) and still nothing.
我没有任何想法了,请解释一下奇怪的行为,为什么会发生这种情况以及 map <<<;[key:object]
和 map[key] = object
?
I don't have any ideas anymore, please can one some explain odd behavior, why is it happening and what is the difference between map << [key:object]
and map[key] = object
?
谢谢.
推荐答案
可能有帮助的一件事是,不要将 GStrings 用于您的键.Groovy 支持将对象直接用括号括起来作为键.
One thing that might help is, don't use GStrings for your keys. Groovy supports using objects directly as keys by wrapping them in parentheses.
来自手册:
默认情况下映射键是字符串:[a:1] 等价于 ["a":1].但是如果你真的想让一个变量成为键,你必须把它用括号括起来:[(a):1].
Map keys are strings by default: [a:1] is equivalent to ["a":1]. But if you really want a variable to become the key, you have to wrap it between parentheses: [(a):1].
完整的示例:
def xml = '<xml><head>headHere</head><body>bodyHere</body></xml>'
Map map1 = [:]
def node = new XmlParser().parseText(xml)
node.each {
map1 << [ (it.name()): it.value() ]
}
println map1
println map1["head"]
println ">>>>>>>>>>>>>>>>>>>>>>"
Map map2 = [:]
map2 << ["head":"headHere"]
map2 << ["body":"bodyHere"]
println map2
println map2["head"]
println "<<<<<<<<<<<<<<<<<<<<<<"
def xml2 = '<xml><head>headHere</head><body>bodyHere</body></xml>'
Map map3 = [:]
def node2 = new XmlParser().parseText(xml2)
node2.each {
map3[it.name()] = it.value()
}
println map3
println map3["head"]
输出为:
[head:[headHere], body:[bodyHere]]
[headHere]
>>>>>>>>>>>>>>>>>>>>>>
[head:headHere, body:bodyHere]
headHere
<<<<<<<<<<<<<<<<<<<<<<
[head:[headHere], body:[bodyHere]]
[headHere]
这篇关于Groovy 中 Map 的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!