Groovy Tutorial: Map Example

by Didin J. on Jun 09, 2025 Groovy Tutorial: Map Example

Master Groovy Maps with hands-on examples—learn how to create, modify, filter, merge, and iterate maps efficiently in a Groovy-powered JVM environment.

Groovy extends Java’s Map interface with concise syntax and powerful new operations, making key-value data manipulation more readable and efficient. Whether you're building a simple key-value store or performing advanced map transformations, this guide covers essential use cases with clear, runnable examples.

Maps are a core feature of the Groovy programming language, building upon Java’s java.util.Map interface to provide more concise and expressive syntax for working with key-value pairs. Like Java maps, Groovy maps store collections of keys associated with specific values, making them ideal for quick data lookups and manipulations. When the key corresponds to an element’s hash code, a map can also function similarly to a set, enhancing its utility in a variety of use cases.


Declare Groovy Map

To declare or create a Groovy map is simpler than using Java. A map can be declared as an empty map or filled the map with default values. Here's an example of an empty map.

def emptyMap = [:]

And here's an example of the filled map with default values.

def userMap = [username: "[email protected]", password: "abcdf12345", fullname: "Didin J."]
def defaultMap = [name: "Eric Cantona", team: "Manchester United", roles: "Striker", age: 28]


Add Item to a Map

Adding the item to a map is very simple. There are a few ways to add items or values to a map. The first way is using the square brackets for the key.

def productMap = [prodName: "iPhone X"]
productMap["prodDesc"] = "A new generation of iPhone X with 8GB RAM and 256 Internal Memory"]
productMap["prodPrice"] = 999

This way useful if you have a dynamic key name, for example, the key name is joined with an index.

def i = 2
def prodMap = [:]
prodMap["prodName" + i] = "Google Pixel XL"
prodMap["prodPrice" + i] = 899

The second way is using a key separate from the map name by a dot (".").

def prodMap = [:]
prodMap.prodName = "Samsung Galaxy S10"
prodMap.prodDesc = "A new generation of the Samsung Galaxy S series"
prodMap.prodPrice = 1099

Or this example also works in Groovy.

def prodMap = [:]
prodMap."prodName" = "Samsung Galaxy S10"
prodMap."prodDesc" = "A new generation of the Samsung Galaxy S series"
prodMap."prodPrice" = 1099

The third way is using the "put" keyword.

def prodMap = [:]
prodMap.put("prodName", "iPhone XS")
prodMap.put("prodDesc", "The latest iPhone series")
prodMap.put("prodPrice", 1199)


Replace the Value of the Map Item

Same as adding the item to the map, replacing map values is very simple.

def playerMap = [name: "Eric Cantona", team: "Manchester United", roles: "Striker", age: 28]
playerMap["name"] = "Robbie Fowler"
playerMap.team = "Liverpool FC"
playerMap."roles" = "Second Striker"
playerMap.put("age", 22)


Remove Item from the Map

Removing an item from the map is also a very easy task. To remove a single item, you can use the "remove" method.

def playerMap = [name: "Eric Cantona", team: "Manchester United", roles: "Striker", age: 28]
playerMap.remove("roles")

Now the map will be like this.

playerMap = [name: "Eric Cantona", team: "Manchester United", age: 28]

To remove multiple map entries or key-value pairs, you can use the "minus" method.

def playerMap = [name: "Eric Cantona", team: "Manchester United", roles: "Striker", age: 28]
playerMap.minus([roles: "Striker", age: 28])

Now the map will contain these items.

playerMap = [name: "Eric Cantona", team: "Manchester United"]


Retrieve Items

There will always be an easy way as other methods, to retrieve items from the map. Here's an example to retrieve an item.

def playerMap = [name: "Eric Cantona", team: "Manchester United", roles: "Striker", age: 28]
def bestPlayer = playerMap["name"]
println bestPlayer

or

def playerMap = [name: "Eric Cantona", team: "Manchester United", roles: "Striker", age: 28]
def bestPlayer = playerMap.name
println bestPlayer

or

def playerMap = [name: "Eric Cantona", team: "Manchester United", roles: "Striker", age: 28]
def bestPlayer = playerMap.get("name")
println bestPlayer

Output:

Eric Cantona


Collect Entries

The Collect Entries method is used to iterate through this Map, transforming each entry using the transform closure and returning a Map of the transformed entries.

def playerMap = [name: "Eric Cantona", team: "Manchester United", roles: "Striker", age: 28]
def colEntries = playerMap.collectEntries { key, value -> [value, key] }
println colEntries

Output:

[Eric Cantona:name, Manchester United:team, Striker:roles, 28:age]

And here's an example of Collect entries where the value is multiplied by 10 times and the key is converted to uppercase.

def playerMap = [name: "Eric Cantona", team: "Manchester United", roles: "Striker", age: 28]
def colEntries2 = playerMap.collectEntries { key, value -> [(value*10): key.toUpperCase()] }
​println colEntries2​

Output:

[Eric CantonaEric CantonaEric CantonaEric CantonaEric CantonaEric CantonaEric CantonaEric CantonaEric CantonaEric Cantona:NAME, Manchester UnitedManchester UnitedManchester UnitedManchester UnitedManchester UnitedManchester UnitedManchester UnitedManchester UnitedManchester UnitedManchester United:TEAM, StrikerStrikerStrikerStrikerStrikerStrikerStrikerStrikerStrikerStriker:ROLES, 280:AGE]

Also, you can create a map from a simple list or array.

def playerList = ["Eric Cantona", "Dennis Bergkamp", "Robbie Fowler"]
def playerMap = playerList.collectEntries{ [(it.length()): it] }
println playerMap

Output:

[12:Eric Cantona, 15:Dennis Bergkamp, 13:Robbie Fowler]

Count Items in the Map

To count a group of items for which the value modulus by 2 equals 0, use the count method.

def numberMap = [nbr1: 11, nbr2: 12, nbr3: 13, nbr4: 14, nbr5: 15]
println numberMap.count { it.value % 2 }

Output:

2

To count a group of items, the value modulus by 2 equals 0 or 1 using the countBy method will result in the group items of which the value modulus by 2 equals 0 as the second item and the group items of which the value modulus by 2 equals 1 as the first item.

def numberMap = [nbr1: 11, nbr2: 12, nbr3: 13, nbr4: 14, nbr5: 15]
println numberMap.countBy { it.value % 2 }

Output:

[1:3, 0:2]


Maps Union

Maps union simple join or unite some maps together as one map.

def wheelsMap = [tire: "Toyo", rims: "Enkei", brake: "EBC"]
def engineMap = [horsepower: "750HP", cc: 3000]
def carMap = wheelsMap + engineMap
println carMap

Output:

[tire:Toyo, rims:Enkei, brake:EBC, horsepower:750HP, cc:3000]


Map Intersect

Map intersect compares two maps and finds the duplicate items as a new map.

def map1 = [a: 1, b: 2, c: 3, d: 4, e: 5]
def map2 = [a: 1, b: 6, c: 3, d: 8, e: 9]
println map1.intersect(map2)

Output:

[a:1, c:3]


Map Iteration

To iterate through the map values, you can use the each and eachWithIndex methods.

def map1 = [a: 1, b: 2, c: 3, d: 4, e: 5]
map1.each { m -> println "$m.key: $m.value" }

Output:

a: 1
b: 2
c: 3
d: 4
e: 5
def map1 = [a: 1, b: 2, c: 3, d: 4, e: 5]
map1.eachWithIndex { m, i ->
  println "$i $m.key: $m.value"
}

Output:

0 a: 1
1 b: 2
2 c: 3
3 d: 4
4 e: 5


Find on the Map

To find items from the map based on a specific value, see this example.

def map1 = [a: 1, b: 2, c: 3, d: 4, e: 5]
def result = map1.findAll { it.value > 2 }
println result

Output:

[c:3, d:4, e:5]

To find items from the map based on a specific key, see this example.

def map1 = [a: 1, b: 2, c: 3, d: 4, e: 5]
def result = map1.findAll { it.key == "c" }
println result

Output:

[c:3]

Conclusion

Groovy maps offer a concise and expressive way to manage key-value data structures, making them essential for modern Groovy development. By extending Java's Map interface, Groovy maps simplify common operations like iteration, filtering, and data transformation. Whether you're building APIs or processing data, understanding how to use maps effectively enhances both code readability and performance. Mastering Groovy maps is a fundamental step for developers aiming to write clean, maintainable, and scalable Groovy applications.

That's the examples of Groovy maps. We are wrapping the examples in one file on GitHub.

That's just the basics. If you need more deep learning about Groovy and Grails, you can take the following cheap course:

Thanks!