Corporate Training
Request Demo
Click me
Menu
Let's Talk
Request Demo

Tutorials

Groovy - Lists and Maps

9. Lists and Maps

Lists and maps are two fundamental data structures in Groovy for organizing and manipulating data. Lists are collections of ordered elements, and maps are collections of key-value pairs.

Lists:

A list in Groovy is an ordered collection of elements. Lists can contain elements of various data types, including numbers, strings, and even other lists.

Creating Lists:

You can create a list in Groovy using square brackets '[]' and separating the elements with commas.

Example:
def fruits = ["apple", "banana", "cherry"]
 

In this example, we created a list named 'fruits' containing three string elements.

Accessing List Elements:

You can access elements of a list by their index, which starts from 0 for the first element.

Example:
def firstFruit = fruits[0]
def secondFruit = fruits[1]
 

In this example, we accessed the first and second elements of the 'fruits'list.

Modifying Lists:

Lists are mutable, meaning you can add, remove, or change elements.

Example:
fruits.add("orange") // Adds "orange" to the end of the list
fruits.remove(1) // Removes the second element ("banana")
fruits[0] = "kiwi" // Changes the first element to "kiwi"
 

Iterating Over Lists:

You can iterate over a list using loops or the 'each'method.

Example (Using a Loop):
for (fruit in fruits) {
    println(fruit)
}
 
Example (Using 'each'Method):
fruits.each { fruit ->
    println(fruit)
}
 

Both examples will print all the elements in the 'fruits'list.

Maps:

A map in Groovy is an unordered collection of key-value pairs. Each key is unique within the map, and you can use it to retrieve the associated value.

Creating Maps:

You can create a map in Groovy using curly braces '{}' and specifying key-value pairs using the ':' separator.

Example:
def person = [name: "Alice", age: 30, city: "New York"]
 

In this example, we created a map named 'person' with keys ('name', 'age', 'city') and their corresponding values.

Accessing Map Elements:

You can access values in a map using their keys.

Example:
def name = person.name
def age = person["age"]
 

In this example, we accessed the 'name' and 'age' values from the 'person' map.

Modifying Maps:

Maps are mutable, so you can add, remove, or update key-value pairs.

Example:
person["city"] = "San Francisco" // Updates the value for the "city" key
person.job = "Engineer" // Adds a new key-value pair
person.remove("age") // Removes the "age" key and its value
 

Iterating Over Maps:

You can iterate over the keys or key-value pairs of a map using loops or the 'each' method.

Example (Iterating Over Keys):
for (key in person.keySet()) {
    println("$key: ${person[key]}")
}
 
Example (Iterating Over Key-Value Pairs):
person.each { key, value ->
    println("$key: $value")
}
 

Both examples will print all the key-value pairs in the 'person' map.

Conclusion:

Lists and maps are versatile data structures in Groovy that allow you to store and manipulate collections of data. Understanding how to create, access, modify, and iterate over lists and maps is essential for working with data effectively in Groovy.