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 October 2025

1 Groovy in Action: Covers Groovy 2.4

Groovy in Action: Covers Groovy 2.4

BUY & SAVE
$28.80 $59.99
Save 52%
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
$30.94 $35.00
Save 12%
Programming Groovy 2: Dynamic Productivity for the Java Developer (Pragmatic Programmers)
3 Making Java Groovy

Making Java Groovy

  • QUALITY ASSURANCE: ALL BOOKS ARE INSPECTED FOR GOOD CONDITION.
  • AFFORDABLE PRICES: ENJOY SAVINGS ON QUALITY USED BOOKS TODAY!
  • SUSTAINABLE CHOICE: RECYCLE BOOKS AND REDUCE ENVIRONMENTAL IMPACT.
BUY & SAVE
$40.14 $44.99
Save 11%
Making Java Groovy
4 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
5 Groovy in Action

Groovy in Action

  • SAME-DAY DISPATCH FOR ORDERS BEFORE 12 PM-FAST DELIVERY!
  • MINT CONDITION GUARANTEED FOR ULTIMATE CUSTOMER SATISFACTION!
  • HASSLE-FREE RETURNS-SHOP RISK-FREE WITH OUR NO QUIBBLES POLICY!
BUY & SAVE
$24.14 $49.99
Save 52%
Groovy in Action
6 Programming Groovy: Dynamic Productivity for the Java Developer (Pragmatic Programmers)

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

  • QUALITY ASSURANCE: CAREFULLY INSPECTED FOR GOOD CONDITION.
  • ECO-FRIENDLY CHOICE: READ, REUSE, AND REDUCE WASTE.
  • AFFORDABLE PRICING: GREAT SAVINGS ON QUALITY LITERATURE!
BUY & SAVE
$34.95
Programming Groovy: Dynamic Productivity for the Java Developer (Pragmatic Programmers)
7 Modern Programming Made Easy: Using Java, Scala, Groovy, and JavaScript

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

BUY & SAVE
$30.95 $37.99
Save 19%
Modern Programming Made Easy: Using Java, Scala, Groovy, and JavaScript
8 The C Programming Language

The C Programming Language

BUY & SAVE
$107.44
The C Programming Language
9 Spock: Up and Running: Writing Expressive Tests in Java and Groovy

Spock: Up and Running: Writing Expressive Tests in Java and Groovy

BUY & SAVE
$46.52 $59.99
Save 22%
Spock: Up and Running: Writing Expressive Tests in Java and Groovy
10 Groovy Oceans Of Possibilities Octopus Summer Reading Book T-Shirt

Groovy Oceans Of Possibilities Octopus Summer Reading Book T-Shirt

  • PERFECT FOR EDUCATORS AND OCEAN LOVERS-BOOST READING ENTHUSIASM!

  • IDEAL FOR SUMMER EVENTS, BOOK FAIRS, AND WORLD BOOK DAY FESTIVITIES!

  • A THOUGHTFUL GIFT FOR TEACHERS AND READERS-MAKE READING FUN!

BUY & SAVE
$17.98
Groovy Oceans Of Possibilities Octopus Summer Reading Book T-Shirt
+
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.