---
title: "Key Manipulation for Map in Groovy "
author: "BenjaminYu"
date: "2014-07-02T12:35:13Z"
tags: ["Groovy", "key-value", "map"]
categories: ["编程语言"]
slug: "key-manipulation-for-map-in-groovy"
draft: false
lastmod: "2014-07-03T12:20:42Z"
id: 2087513
url: "https://benjaminyu.iteye.com/blog/2087513"
---

# [Get key in groovy maps](http://stackoverflow.com/questions/4898062/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
```

```
 
```

 

12[down vote]()accepted

try [`map.keySet()`](http://download.oracle.com/javase/6/docs/api/java/util/Map.html#keySet%28%29)

and if you want an array:

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

Or, more groovy-ish:

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

[share](http://stackoverflow.com/a/4898086 "short permalink to this answer")|[improve this answer](http://stackoverflow.com/posts/4898086/edit)

[edited Feb 4 '11 at 13:09](http://stackoverflow.com/posts/4898086/revisions "show all edits to this post")

 

 

answered Feb 4 '11 at 12:42

![](https://www.gravatar.com/avatar/65a1e72ed7a72ed7b0831fbcd1fee8b0?s=32&d=identicon&r=PG)

[Sean Patrick Floyd](http://stackoverflow.com/users/342852/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]