获取 groovy 映射中的密钥 [英] Get key in groovy maps
本文介绍了获取 groovy 映射中的密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
def map = [name:"Gromit", likes:"cheese", id:1234]
我想以获取密钥的方式访问地图
I would like to access map in such a way that I can get the key
类似于输出的东西
map.keys 返回字符串数组.基本上我只想拿到钥匙
map.keys returns array of string. basically i just want to get the keys
输出:
name
likes
id
推荐答案
try map.keySet()
如果你想要一个数组:
map.keySet() as String[]; // thx @tim_yates
或者,更时髦:
map.each{
key, value -> print key;
}
警告:在 Jenkins 中,这个 groovy-ish 示例被巧妙地破坏了,因为它依赖于迭代器.迭代器在 Jenkins Pipeline 代码中并不安全,除非包装在 @NonCPS 函数中.
Warning: In Jenkins, the groovy-ish example is subtly broken, as it depends on an iterator. Iterators aren't safe in Jenkins Pipeline code unless wrapped in a @NonCPS function.
这篇关于获取 groovy 映射中的密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文