在Groovy中使用Map的奇怪行为 [英] Odd behaviour with Map in Groovy
问题描述
我正在编写代码,并且在处理XML和Maps时,我注意到了Groovy中的一些奇怪行为。我考虑过了,不知道为什么会发生这种情况,应该如此。
我用3个示例编写了示例代码。 map1& amp; amp; map3仅限于以下部分:
Map1:
map1 << [$ {it.name()}:it.value()]
Map3: / p>
map3 [$ {it.name()}] = it.value()
$ c
以下是完整的代码,您可以将其复制粘贴到Groovy控制台中:
def xml ='< xml>< head> headHere< / head>< body> bodyHere< / body>< / xml>'
Map map1 = [:]
def node = new XmlParser()。parseText(xml)
$ b $ 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<<<< <<<<<<<<<<<<<<<<<
$ b 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),但仍然没有任何东西。
我没有任何想法了,请解释一下奇怪的行为,为什么它发生了什么, map<<< [key:object]
和 map [key] = object
?
谢谢。
可能有帮助的一件事是,不要将GStrings用于您的密钥。 Groovy支持直接使用对象作为关键字,方法是将它们包装在圆括号中。
From :
Map键默认是字符串:[a:1]等同于[ A:1]。但是,如果你真的想要一个变量成为关键,你必须将它包装在圆括号之间:[(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<<<< <<<<<<<<<<<<<<<<<
$ b $ 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
<< b << b << b << b << b < <<<<<<<<
[head:[headHere],body:[bodyHere]]
[headHere]
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.
I wrote sample code with 3 examples. Crucial difference between map1 & map3 is only on the following part:
Map1:
map1 << ["${it.name()}":it.value()]
Map3:
map3["${it.name()}"]=it.value()
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"]
The result that I am getting is following:
[head:[headHere], body:[bodyHere]]
null
[head:headHere, body:bodyHere]
headHere
[head:[headHere], body:[bodyHere]]
[headHere]
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.
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
?
Thank you.
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.
From the manual:
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].
Fully working example:
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"]
The output is:
[head:[headHere], body:[bodyHere]]
[headHere]
>>>>>>>>>>>>>>>>>>>>>>
[head:headHere, body:bodyHere]
headHere
<<<<<<<<<<<<<<<<<<<<<<
[head:[headHere], body:[bodyHere]]
[headHere]
这篇关于在Groovy中使用Map的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!