Skip to main content
TopMiniSite

Back to all posts

How to Iterate Over A Collection In Groovy?

Published on
4 min read
How to Iterate Over A Collection In Groovy? image

Best Groovy Programming Guides to Buy in December 2025

1 Groovy in Action: Covers Groovy 2.4

Groovy in Action: Covers Groovy 2.4

BUY & SAVE
$57.19 $59.99
Save 5%
Groovy in Action: Covers Groovy 2.4
2 Programming Groovy 2: Dynamic Productivity for the Java Developer (Pragmatic Programmers)

Programming Groovy 2: Dynamic Productivity for the Java Developer (Pragmatic Programmers)

BUY & SAVE
$26.98 $35.00
Save 23%
Programming Groovy 2: Dynamic Productivity for the Java Developer (Pragmatic Programmers)
3 Groovy Programming: An Introduction for Java Developers

Groovy Programming: An Introduction for Java Developers

BUY & SAVE
$58.56 $65.95
Save 11%
Groovy Programming: An Introduction for Java Developers
4 Making Java Groovy

Making Java Groovy

  • QUALITY ASSURANCE: ALL BOOKS ARE CAREFULLY INSPECTED FOR READABILITY.
  • AFFORDABLE PRICES: SAVE MONEY WHILE ENJOYING GREAT LITERATURE.
  • ECO-FRIENDLY CHOICE: CONTRIBUTE TO SUSTAINABILITY WITH EVERY PURCHASE.
BUY & SAVE
$44.32
Making Java Groovy
5 Groovy in Action

Groovy in Action

  • SAME-DAY DISPATCH FOR ORDERS BEFORE NOON-FAST DELIVERY!
  • MINT CONDITION ASSURED-GET TOP-QUALITY PRODUCTS EVERY TIME!
  • HASSLE-FREE RETURNS-SHOP WITH CONFIDENCE AND PEACE OF MIND!
BUY & SAVE
$41.97 $49.99
Save 16%
Groovy in Action
6 Modern Programming Made Easy: Using Java, Scala, Groovy, and JavaScript

Modern Programming Made Easy: Using Java, Scala, Groovy, and JavaScript

BUY & SAVE
$30.75 $37.99
Save 19%
Modern Programming Made Easy: Using Java, Scala, Groovy, and JavaScript
7 Programming Groovy: Dynamic Productivity for the Java Developer (The Pragmatic Programmers)

Programming Groovy: Dynamic Productivity for the Java Developer (The Pragmatic Programmers)

  • QUALITY ASSURANCE: EACH BOOK IS INSPECTED FOR GOOD CONDITION.
  • ECO-FRIENDLY CHOICE: SAVE MONEY AND RESOURCES WITH PRE-LOVED BOOKS.
  • FAST SHIPPING: GET YOUR USED BOOK DELIVERED QUICKLY TO YOUR DOOR.
BUY & SAVE
$34.95
Programming Groovy: Dynamic Productivity for the Java Developer (The Pragmatic Programmers)
8 Groovy Recipes: Greasing the Wheels of Java

Groovy Recipes: Greasing the Wheels of Java

BUY & SAVE
$71.03
Groovy Recipes: Greasing the Wheels of Java
9 Groovy: Grundlagen und fortgeschrittene Techniken (German Edition)

Groovy: Grundlagen und fortgeschrittene Techniken (German Edition)

BUY & SAVE
$50.00
Groovy: Grundlagen und fortgeschrittene Techniken (German Edition)
10 No Static: A Guide to Creative Radio Programming

No Static: A Guide to Creative Radio Programming

  • QUALITY ASSURANCE: EACH BOOK IS THOROUGHLY INSPECTED FOR QUALITY.
  • BUDGET-FRIENDLY: ENJOY SIGNIFICANT SAVINGS ON GENTLY-USED TITLES.
  • ECO-FRIENDLY CHOICE: CONTRIBUTE TO SUSTAINABILITY BY BUYING USED!
BUY & SAVE
$24.95
No Static: A Guide to Creative Radio Programming
+
ONE MORE?

To iterate over a collection in Groovy, you can use the each() method or a for loop. The each() method allows you to iterate over each element in the collection and perform some operation on it. You can also use the collect() method to transform each element of the collection.

Alternatively, you can use a for loop with the in keyword to iterate over the elements of the collection. Inside the loop, you can access each element using the loop variable.

Here is an example using the each() method to iterate over a list:

def numbers = [1, 2, 3, 4, 5]

numbers.each { println it }

This will print each element in the list on a new line.

You can also use a for loop to achieve the same result:

def numbers = [1, 2, 3, 4, 5]

for (num in numbers) { println num }

Both methods allow you to iterate over a collection in Groovy and perform operations on each element.

How to iterate over a collection in Groovy using the collect() method?

In Groovy, the collect() method is used to iterate over a collection and apply a transformation to each element in the collection. Here's an example of how to use the collect() method to iterate over a list of numbers and square each number:

def numbers = [1, 2, 3, 4, 5]

def squares = numbers.collect { it * it }

println squares // Output: [1, 4, 9, 16, 25]

In the example above, we have a list of numbers [1, 2, 3, 4, 5]. We use the collect() method on this list and provide a closure { it * it } that squares each element in the list. The result is stored in the squares variable, which contains [1, 4, 9, 16, 25].

You can also use the collect() method to apply more complex transformations to the elements in the collection. Just replace the closure with the desired transformation logic.

What is the benefit of using the each() method for iterating over a collection in Groovy?

The each() method in Groovy allows for a more concise and expressive way of iterating over a collection compared to traditional for-loops.

Some benefits of using the each() method include:

  1. It simplifies the code and makes it more readable by avoiding the use of index variables and manual iteration.
  2. It provides a more functional approach to iterating over collections, allowing for the use of closures or lambda expressions to perform actions on each element in the collection.
  3. It can be used with a variety of different collection types, including lists, maps, and ranges.
  4. It often results in more efficient code as the iteration logic is handled by the each() method implementation, which is optimized for performance.

What is the function of the collect() method when iterating over a collection in Groovy?

The collect() method in Groovy is used to transform each element in a collection based on a defined closure or function and returns a new collection with the transformed elements.

For example, if you have a list of numbers and you want to double each number in the list, you can use the collect() method like this:

def numbers = [1, 2, 3, 4, 5] def doubledNumbers = numbers.collect { it * 2 }

println doubledNumbers // Output: [2, 4, 6, 8, 10]

In this example, the collect() method applies the given closure { it * 2 } to each element in the numbers list, multiplying each element by 2. The resulting list doubledNumbers contains the transformed elements.

How to iterate over a collection in Groovy using the sort() method?

In Groovy, you can use the sort() method to iterate over a collection and sort its elements. Here is an example of how you can use the sort() method to iterate over a list of numbers:

def numbers = [5, 2, 8, 1, 9]

// Sort the numbers in ascending order numbers.sort { a, b -> a <=> b }

// Iterate over the sorted numbers numbers.each { println it }

In this example, the sort() method is called on the list of numbers, and a closure is passed to define the sorting criterion (in this case, sorting the numbers in ascending order). The sorted numbers are then iterated over using the each() method.

You can also simplify the code by chaining the sort() and each() methods:

def numbers = [5, 2, 8, 1, 9]

numbers.sort { a, b -> a <=> b }.each { println it }

This will achieve the same result of sorting the numbers in ascending order and iterating over them.