Groovy Tutorial: Loop Examples

by Didin J., updated on Nov 20, 2021 Groovy Tutorial: Loop Examples

The collection of Groovy Loops through Array, List, Collection examples with source codes


Looping is an essential feature in every Programming Language including Groovy. Loops a control flow statement for traversing or iterating items in a collection, array, list, set, etc. There's a lot of ways to loop through collection, array, list, or set in Groovy. It can use classic for, do-while, while, for-in, each, or times loop. This time, we will show you the examples that used in the real Groovy projects.

Table of Contents:

Before jumping to the example, make sure you have to install Java Virtual Machine and the Groovy SDK from Apache Groovy Official https://groovy.apache.org/download.html. Then set the groovy command to the environment variable. In Mac, we can use SDKMan to install the Groovy SDK.

sdk install groovy

Now, we can run our Groovy file using the groovy command.

groovy myGroovyExample.groovy

We are using Groovy Version: 3.0.9 and JVM: 11.0.11. There is some Groovy compiler online, but make sure their version is using the latest Groovy version to make this tutorial work.


For Loop Examples

The classical looping thought the collection, array, list, or set in Groovy/Java/C is "for" keyword.

Classic For Loop example:

def cars = ["BMW", "Mercedes Benz", "Volvo", "Chevrolet", "Ford"]

for (int i = 0; i < cars.size(); i++) {
    println cars[i]
}

Results:

BMW
Mercedes Benz
Volvo
Chevrolet
Ford

For loop with comma-separated expressions example:

for (int i = 1, j = 10; i < j; i++, j--) {
    println(i + ', ' + j)
}

Results:

1, 10
2, 9
3, 8
4, 7
5, 6

For loop multi assignment example:

def sku = []
for (def (String a, int b) = ['AA', 1000]; b < 1010; a++, b++) {
    sku << "$a$b"
}
println sku

Results:

[AA1000, AB1001, AC1002, AD1003, AE1004, AF1005, AG1006, AH1007, AI1008, AJ1009]


For In Loop Examples

Iterate over list of objects example:

@groovy.transform.Canonical
class Cars {
    String brand = ""
    String type = ""
    Integer price = 0 
}

List<Cars> cars = [
    [brand: "BMW", type: "750i", price: 300000],
    [brand: "Mercedes Benz", type: "B600", price: 120000],
    [brand: "Volvo", type: "S230", price: 98000]
]

for (car in cars) {
    println "$car.brand $car.type -> $car.price"
}

Result:

BMW 750i -> 300000
Mercedes Benz B600 -> 120000
Volvo S230 -> 98000

Iterate over an array of string examples:

def carNameArray = ["BMW", "Mercedez Benz", "Volvo"]

for (car in carNameArray) {
    println car
}

Result:

BMW
Mercedez Benz
Volvo

Iterate over range or number example:

for ( i in 0..5 ) {
    println "Line number: $i"
}

Result:

Line number: 0
Line number: 1
Line number: 2
Line number: 3
Line number: 4
Line number: 5

Iterate over map example:

def map = ['BMW': 300000, 'Mercedez Benz': 200000, 'Volvo': 90000]

for ( m in map ) {
    println "$m.key -> $m.value" 
}

Result:

BMW -> 300000
Mercedez Benz -> 200000
Volvo -> 90000

Iterate over map values example:

def map2 = ['BMW': 300000, 'Mercedez Benz': 200000, 'Volvo': 90000]

for ( m2 in map2.values() ) {
    println m2
}

Result:

300000
200000
90000

Iterate over the characters in the string example:

def codeName = "abcdef0123456789"

for (s in codeName) {
    print "$s "
}

Result:

a b c d e f 0 1 2 3 4 5 6 7 8 9


While Loop Examples

Iterate over an array of strings using classic while example:

def cars = ["BMW", "Mercedez Benz", "Volvo"]
def x = 0

while ( x < cars.size() ) {
    println(cars[x])
    x++
}

Result:

BMW
Mercedez Benz
Volvo


Do/While Loop Examples

Classic Java-style do/while example:

def cars = ["BMW", "Mercedez Benz", "Volvo"]
def x = 0

do {
    println cars[x]
    x++
} while(cars.size() > x)

Result:

BMW
Mercedez Benz
Volvo


Each Loop Examples

Iterate over list of objects with implicit parameter "it" example:

@groovy.transform.Canonical
class Cars {
    String brand = ""
    String type = ""
    Integer price = 0 
}

List<Cars> cars = [
    [brand: "BMW", type: "750i", price: 300000],
    [brand: "Mercedes Benz", type: "B600", price: 120000],
    [brand: "Volvo", type: "S230", price: 98000]
]

cars.each {
    println "$it.brand $it.type -> $it.price"
}

Result:

BMW 750i -> 300000
Mercedes Benz B600 -> 120000
Volvo S230 -> 98000

Iterate over a list of objects with variable examples:

cars.each { c ->
    println "$c.brand $c.type -> $c.price"
}

Result:

BMW 750i -> 300000
Mercedes Benz B600 -> 120000
Volvo S230 -> 98000

Iterate over list of objects with index example:

cars.eachWithIndex { c, idx ->
    println idx+1 + ". $c.brand $c.type -> $c.price"
}

Result:

1. BMW 750i -> 300000
2. Mercedes Benz B600 -> 120000
3. Volvo S230 -> 98000


Times Loop Examples

Iterate over list of objects using times example:

@groovy.transform.Canonical
class Cars {
    String brand = ""
    String type = ""
    Integer price = 0 
}

List<Cars> cars = [
    [brand: "BMW", type: "750i", price: 300000],
    [brand: "Mercedes Benz", type: "B600", price: 120000],
    [brand: "Volvo", type: "S230", price: 98000]
]

cars.size().times { it ->
    println cars[it]
}

Result:

[brand:BMW, type:750i, price:300000]
[brand:Mercedes Benz, type:B600, price:120000]
[brand:Volvo, type:S230, price:98000]

That it's, the examples of Groovy map. We are wrapping the examples in one file in GitHub.

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

Thanks!

Loading…