Fundamentals 4 min read

Mastering Groovy Tuples: Immutable Collections and Practical Examples

This article explains Groovy's immutable Tuple classes, shows how to create and use Tuple, Tuple2, Tuple3, and demonstrates their read‑only behavior with practical code examples, highlighting why they are ideal for returning multiple values from methods.

FunTester
FunTester
FunTester
Mastering Groovy Tuples: Immutable Collections and Practical Examples

Groovy provides ordered, immutable collections called tuples through the groovy.lang.Tuple class. A tuple instance is created by passing all desired elements to the constructor, and once created its size and contents cannot be changed—no elements can be added, removed, or modified. This immutability makes tuples especially useful as method return values when multiple results need to be conveyed.

For the common case of two elements, Groovy offers the Tuple2 class, which enforces the element types at compile time. Similar specialized classes exist for three or more elements (e.g., Tuple3).

Below is a complete example that demonstrates creation, access, and the immutable nature of tuples:

package com.FunTester.demo

import com.fun.frame.SourceCode

class demo5 extends SourceCode {
    public static void main(String[] args) {
        def tuple = new Tuple('one', 1, getJson("demo=1"))
        println tuple.size() == 3
        println tuple.get(0) == 'one'
        println tuple[1] == 1
        println tuple.last().demo == 1
        // Attempt to modify the tuple
        try {
            tuple.add('extra')
            println false
        } catch (Exception e) {
            println e
        }
        try {
            tuple.remove('one')
            println false
        } catch (Exception e) {
            println e
        }
        try {
            tuple[0] = 'new value'
            println false
        } catch (Exception e) {
            println e
        }
        // Tuple2 demo
        def pair = new Tuple2('two', 2)
        println pair.first == 'two'
        println pair.second == 2
        // Tuple3 demo
        def tuple3 = new Tuple3("true", 3, 4)
        println tuple3.third
        // Multiple assignment using a helper method
        def (String a, Integer b) = dd('sum', 1, 2, 3)
        println a == 'sum'
        println b == 6
    }

    static def dd(String key, int... values) {
        new Tuple2(key, values.sum())
    }
}

The code shows that any attempt to call add, remove, or assign a new value to an index results in an exception, confirming the tuple's read‑only contract. It also illustrates how Tuple2 and Tuple3 can be used, and how a helper method ( dd) can return a Tuple2 that is immediately de‑structured into separate variables.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendJavaprogrammingGroovytupleImmutable
FunTester
Written by

FunTester

10k followers, 1k articles | completely useless

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.