`

Key Manipulation for Map in Groovy

阅读更多

Get key in groovy maps

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 output should be

map.keys returns array of string. basically i just want to get the keys

output:

name
likes
id
 

 

12down voteaccepted

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;
}
share|improve this answer

 

Get the last key of a Map

map.keySet().last() or map.keySet()[-1]

 

Get sub-map consists of  the last key-value of a Map

map.subMap(map.keySet().last())

 

Get sub-map except the last key-value

map.minus(map.subMap(map.keySet().last()))

 

Get the last item of a list

list.last()

Get the sub-list without the last element

list.take(list.size()-1)   or list[0..list.size()-1]

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics