HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to GroovyVariablesData TypesOperatorsStringsListsConditions (if / else)Loops (for / while)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables & Types
🔢 Numbers & Math
✍️ String
📚 List & Map
🔀 Condition & Loop
🧩 Function
Groovy›Lists

Lists

List creation, element access, adding (<<), length (size), and iterating with loops.

Simple enough for a 5-year-old to understand

Create and access a list

A list is created with square brackets. Indexing starts at 0.

Example code
1
def languages = ["Groovy", "Go", "Rust"]
A list with three elements.
VariableA list with three elements.
2
println languages[0]
Groovy (first element)
Output / InputGroovy (first element)
3
println languages.size()
3 (number of elements)
Output / Input3 (number of elements)
4
println languages
[Groovy, Go, Rust]
Output / Input[Groovy, Go, Rust]
VariableOutput / Input
💡 Hint: Number of elements .size(), first element items[0].

About this topic

Writing a list in Groovy is as easy as using square brackets; behind the scenes, a Java list is created. The language has its own concise methods for adding, filtering, and transforming elements.

Collection methods are Groovy's biggest time-savers. Applying an operation to each element, selecting matching ones, transforming, and grouping are all single-line expressions; the boilerplate Java requires for the same tasks is completely eliminated.

The most common mistake here is confusing transformation and traversal methods. One returns a new collection, while the other merely performs an action for each element and discards the result.

What you will learn

  • Creating lists and accessing elements
  • Adding and removing elements
  • Transformation and filtering methods
  • Using negative indices
  • The difference between traversal and transformation

Who is this for?

  • Script writers working with collections
  • Those looking to get rid of Java loops
  • Those getting empty results while expecting a return value

When to use?

In build scripts, file lists and dependency sets are processed using collection methods; writing loops is rarely necessary.

Use the traversal method if you only want a side effect, and the transformation method if you want a new collection.

Common mistakes

Wrong

def liste = [1, 2, 3]
def kareler = liste.each { it * it }
println kareler

The traversal method runs the block for each element but does not collect the result; it returns the original list. The screen prints the original list, not the squares. If you want a new collection, you must use the transformation method.

Correct

def liste = [1, 2, 3]
def kareler = liste.collect { it * it }
println kareler

Wrong

def liste = [1, 2, 3]
println liste[-1]
println liste[5]

A negative index in Groovy counts from the end and works seamlessly; however, a positive index out of range throws an IndexOutOfBoundsException. The difference between the two behaviors can be surprising. When dealing with external indices, it is necessary to check bounds before accessing.

Correct

def liste = [1, 2, 3]
println liste[-1]
def i = 5
println (i < liste.size() ? liste[i] : "out of range")

Wrong

def liste = [1, 2, 3, 4]
liste.each { sayi ->
  if (sayi % 2 == 0) liste.remove(sayi)
}

Modifying a collection while traversing it throws a ConcurrentModificationException or causes elements to be skipped. The correct approach is to generate a new list from the non-matching ones or use the method that directly removes the matching ones.

Correct

def liste = [1, 2, 3, 4]
def tekler = liste.findAll { it % 2 != 0 }
println tekler

Frequently asked questions

What is the default block parameter?

If you do not write a parameter name in blocks, Groovy assigns a default name to the single parameter. It simplifies code in short expressions; in nested blocks, you should provide an explicit name because it becomes unclear which value is being referenced.

How do I add an element to a list?

The addition operator appends an element to the end and can be chained. To combine two lists, the plus operator is used; this produces a new list and does not modify the original. The difference between the two is important regarding side effects.

How is grouping done?

You pass a block that produces a key to the grouping method; the result is a map from keys to lists. When generating reports and categorizing records, this is much shorter than writing loops.

Summary

Lists are written with square brackets.

The traversal method does not return a new collection.

The transformation method produces a new collection.

Negative indices count from the end.

Modifying a collection while traversing it leads to errors.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code