我有一个groovy方法,适用于 map containing maps
的硬编码变量 . 我想做,这样 Map 作为参数传递 . Map 的数量也将是 vary
. 我想要实现的简单表示将是这样的:
def name(Map p...) {
//code to loop through each of the maps
p.each { k ->
"${k.first}, ${k.last}"
//another loop with the internal map
something {
k.details.each { name, value ->
//some code
}
}
}
}
和 Map of maps
的例子,我需要传递,因为Args看起来像这样:
def persons = [
[first: 'Jack', last: 'Smith', details: [gender: 'male', Age: 25]],
[first: 'Sean', last: 'Dean', details: [gender: 'male', Age: 26]]
]
然后下线,我想打电话
name(persons)
我怎么能实现这个目标?到目前为止我在groovyConsole中的测试并没有把我带到任何地方......
2 回答
我认为问题是,你没有 Map Map 而是 Map 列表 . 因此,为了能够以person作为参数调用您的方法,您必须将其签名更改为:
这是我在groovyConsole中的片段:
问题是你要将
list
传递给varArgs
,你必须使用*(list)
从列表中提取每个元素并传递它们:例:
注意:我不太确定我使用的是正确的术语,但我希望你能得到主旨
:)