Groovy 中 Map 的奇怪行为

编程入门 行业动态 更新时间:2024-10-28 00:14:35
本文介绍了Groovy 中 Map 的奇怪行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在编写代码时注意到 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 的奇怪行为

本文发布于:2023-10-14 11:33:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1490951.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:奇怪   Groovy   Map

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!