ITeye博客

Key Manipulation for Map in Groovy

BenjaminYu · 2014-07-02 12:35 · Groovy · 编程语言 · Markdown

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

edited Feb 4 '11 at 13:09

 

 

answered Feb 4 '11 at 12:42

Sean Patrick Floyd
107k19167300

 

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]

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。