在Groovy中使用CollectEntries创建地图 [英] Create Map with CollectEntries in Groovy
问题描述
我有以下列表: appList = [DevOpsApplication,01.01.01]
I have the following list: appList = [DevOpsApplication, 01.01.01]
我想使用collectEntries
创建地图.我知道it
是指迭代的当前元素({ it -> it }
的快捷方式).因此,我尝试使用索引:
I would like to create a map using collectEntries
. I know that it
refers to the current element of an iteration (shortcut for { it -> it }
). Therefore, I tried to use the index:
def appMap = appList.collectEntries { [(it[0]):it[1]] }
但是,这给了我
[D:e, 0:1]
但是我想要[DevOpsApplication: 01.01.01]
.有办法吗?
But I want [DevOpsApplication: 01.01.01]
. Is there a way to do this?
此外,将来我希望将其扩展到2个以上的元素(例如[DevOpsApplication, 01.01.01, AnotherDevOpsApplication, 02.02.02]
),并具有所需的[DevOpsApplication: 01.01.01, AnotherDevOpsApplication: 02.02.02]
输出.
这怎么可能?
Additionally, In future I would like this to expand to more than 2 elements (e.g. [DevOpsApplication, 01.01.01, AnotherDevOpsApplication, 02.02.02]
) with the desired output of [DevOpsApplication: 01.01.01, AnotherDevOpsApplication: 02.02.02]
.
How will this be possible?
推荐答案
我还找到了另一种方法. Groovy可以转换Object数组的值,并使用toSpreadMap()
将它们转换为映射.但是,数组必须具有偶数个元素.
I also found another method. Groovy can convert the values of an Object array and convert them into a map with the toSpreadMap()
. However, the array must have an even number of elements.
def appList = ['DevOpsApplication', '01.01.01']
def appMap = appList.toSpreadMap()
这篇关于在Groovy中使用CollectEntries创建地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!