Lists, Maps and Ranges
Site Admin
· 11 Sep 2026
· 6 views
Groovy Lists
Groovy provides enhanced list handling with the def keyword and a rich API for collection manipulation:
// Create lists
def fruits = ["apple", "banana", "cherry"]
def numbers = [1, 2, 3, 4, 5]
// Access elements
println fruits[0] // apple
println fruits.last() // cherry
// Add and remove
fruits << "date" // Append
def top3 = fruits.take(3)
List Operations
Groovy lists support functional operations out of the box:
def nums = [1, 2, 3, 4, 5]
def doubled = nums.collect { it * 2 }
// [2, 4, 6, 8, 10]
def evens = nums.findAll { it % 2 == 0 }
// [2, 4]
def sum = nums.sum()
// 15
nums.each { println it }
Maps
Maps in Groovy use a simple syntax and support dot-notation access:
def person = [name: "Alice", age: 30, city: "Paris"]
println person.name // Alice
println person["age"] // 30
person << [email: "alice@example.com"]
Ranges
Ranges create sequences of values and are useful for iteration and slicing:
def oneToTen = 1..10
println oneToTen.collect { it * it }
// [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
// String ranges
def letters = "a".."f"
println letters.toList()
// [a, b, c, d, e, f]
Key Points
- Groovy lists use square bracket syntax and support rich operations.
collect,findAll, andeachare powerful iteration methods.- Maps use colon syntax and support dot-notation access.
- Ranges create sequences with
..operator. - The
<<operator appends to lists and maps.