DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Top 10 C# Keywords and Features
  • Why and How To Integrate Elastic APM in Apache JMeter
  • Enhancing Code Clarity With Python Namedtuples
  • Transitioning From Groovy to Kotlin for Gradle Android Projects

Trending

  • Teradata Performance and Skew Prevention Tips
  • Java 23 Features: A Deep Dive Into the Newest Enhancements
  • How to Build Local LLM RAG Apps With Ollama, DeepSeek-R1, and SingleStore
  • Segmentation Violation and How Rust Helps Overcome It
  1. DZone
  2. Coding
  3. Languages
  4. Groovy Goodness: Using Tuples

Groovy Goodness: Using Tuples

Learn all about using tuples, which are ordered, immutable elements lists, in Groovy.

By 
Hubert Klein Ikkink user avatar
Hubert Klein Ikkink
·
Mar. 13, 16 · Tutorial
Likes (6)
Comment
Save
Tweet
Share
27.4K Views

Join the DZone community and get the full member experience.

Join For Free

A tuple is an ordered, immutable list of elements. Groovy has its own groovy.lang.Tuple class. We can create an instance of a Tuple by providing all elements that need to be in the Tuple via the constructor. We cannot add new elements to a Tuple instance or remove elements. We cannot even change elements in a tuple, so it is completely immutable. This makes it very usable as the return value for a method where we need to return multiple values. Groovy also provides a Tuple2 class that can be used for tuple instance of only two elements. The elements are typed in a Tuple2 instance.

In the following example we see different uses of the Tuple and Tuple2 classes:

def tuple = new Tuple('one', 1, new Expando(number: 1))

assert tuple.size() == 3

// To get the value of an element
// at a certain position we use
// the get(index) method.
assert tuple.get(0) == 'one'

// We can use the [] syntax to
// get elements from the tuple.
assert tuple[1] == 1

// We can use methods added to the
// Collection API by Groovy.
assert tuple.last().number == 1

// We cannot change the tuple.
try {
    tuple.add('extra')
    assert false
} catch (UnsupportedOperationException e) {
    assert e
}

try {
    tuple.remove('one')
    assert false
} catch (UnsupportedOperationException e) {
    assert e
}

try {
    tuple[0] = 'new value'
    assert false
} catch (UnsupportedOperationException e) {
    assert e
}


// Create a Tuple with fixed size 
// of 2 elements, a pair.
def pair = new Tuple2('two', 2)

// The Tuple2 class has extra methods
// getFirst() and getSecond() to 
// access the values.
assert pair.first == 'two'
assert pair.second == 2

An example of how to use a Tuple2 as return value for a method:

def calculate(String key, Integer... values) {
    // Method return a Tuple2 instance.
    new Tuple2(key, values.sum())
}

// Use multiple assignment to
// extract the values from the tuple.
// Tuple2 has typed objects.
def (String a, Integer b) = calculate('sum', 1, 2, 3)

assert a == 'sum'
assert b == 6

Written with Groovy 2.4.6.

Groovy (programming language) Tuple

Published at DZone with permission of Hubert Klein Ikkink, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Top 10 C# Keywords and Features
  • Why and How To Integrate Elastic APM in Apache JMeter
  • Enhancing Code Clarity With Python Namedtuples
  • Transitioning From Groovy to Kotlin for Gradle Android Projects

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: