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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Beyond Algorithms: The Human Element in AI-Driven Cybersecurity
  • The Unreasonable Effectiveness of the Actor Model for Creating Agentic LLM Applications
  • Migrating from React Router v5 to v6: A Comprehensive Guide
  • Playwright: Filter Visible Elements With locator.filter({ visible: true })

Trending

  • ScyllaDB in Enterprise Java: High-Performance NoSQL With Eclipse JNoSQL
  • Replay, Scheduling, and Throttling: Engineering a Self-Service Kafka Streaming Tool at Scale
  • The Window Closes: Why I Switched to Kotlin Before the Codebase Got Too Big
  • After the AI Commit: What Eight Retrospectives Taught Me About Working With Coding Agents

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Languages
  4. Groovy Goodness: Transform Elements While Flattening [Snippet]

Groovy Goodness: Transform Elements While Flattening [Snippet]

Learn more about elements in this Groovy snippet.

By 
Hubert Klein Ikkink
·
Jan. 14, 20 · Code Snippet
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
8.3K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Learn more about elements in this Groovy snippet.
We can use the flatten method in Groovy to flatten a collection that contains other collections into a single collection with all elements. We can pass a closure as an extra argument to the flatten method to transform each element that is flattened. The argument of the closure is the element from the original collection.
You may also like: [DZone Refcard] Getting Started With Groovy

In the following example, we first use the flatten method without a closure argument. Then we pass a closure argument and transform the element:

Groovy




x


 
1
def list = [1, [2, 3], [[4]]]
2
 
          
3
// Simple flatten the nested collections.
4
assert list.flatten() == [1, 2, 3, 4]
5
 
          
6
// We can use a closure to transform
7
// the elements in the resulting collection.
8
assert list.flatten { it * 2 } == [2, 4, 6, 8]



Written with Groovy 2.5.7.

Further Reading

[DZone Refcard] Getting Started With Groovy

Statically Compiled Groovy: Give Groovy a Chance

Groovy (programming language) Element Snippet (programming)

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

Opinions expressed by DZone contributors are their own.

Related

  • Beyond Algorithms: The Human Element in AI-Driven Cybersecurity
  • The Unreasonable Effectiveness of the Actor Model for Creating Agentic LLM Applications
  • Migrating from React Router v5 to v6: A Comprehensive Guide
  • Playwright: Filter Visible Elements With locator.filter({ visible: true })

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook