Get key in groovy maps

后端 未结 1 1638
野性不改
野性不改 2020-12-30 18:51
def map = [name:\"Gromit\", likes:\"cheese\", id:1234]

I would like to access map in such a way that I can get the key

something like the o

相关标签:
1条回答
  • 2020-12-30 19:09

    try map.keySet()

    and if you want an array:

    map.keySet() as String[]; // thx @tim_yates
    

    Or, more groovy-ish:

    map.each{
        key, value -> print key;
    }
    

    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.

    0 讨论(0)
提交回复
热议问题