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

Related

  • Beyond Algorithms: The Human Element in AI-Driven Cybersecurity
  • Observability for the Invisible: Tracing Message Drops in Kafka Pipelines
  • The Unreasonable Effectiveness of the Actor Model for Creating Agentic LLM Applications
  • Migrating from React Router v5 to v6: A Comprehensive Guide

Trending

  • Stop Using Python for Your GenAI Apps, Use Go and Genkit Instead
  • We Went Multi-Cloud and Almost Drowned: Lessons From Running Across AWS, GCP, and Azure
  • Why AI Forces a Rethink of Everything We Know About Software Security
  • Comparing Top Gen AI Frameworks for Java in 2026
  1. DZone
  2. Coding
  3. Languages
  4. Groovy Goodness: Drop or Take Elements with Condition

Groovy Goodness: Drop or Take Elements with Condition

By 
Hubert Klein Ikkink user avatar
Hubert Klein Ikkink
·
Oct. 11, 12 · Interview
Likes (0)
Comment
Save
Tweet
Share
6.0K Views

Join the DZone community and get the full member experience.

Join For Free

In Groovy we can use the drop() and take() methods to get elements from a collection or String object. Since Groovy 1.8.7 we also can use the dropWhile() and takeWhile() methods and use a closure to define a condition to stop dropping or taking elements. With the dropWhile() method we drop elements or characters until the condition in the closure is true. And the takeWhile() method returns elements from a collection or characters from a String until the condition of the closure is true.

In the following example we see how we can use the methods:

def s = "Groovy Rocks!"

assert s.takeWhile { it != 'R' } == 'Groovy '
assert s.dropWhile { it != 'R' } == 'Rocks!'


def list = 0..10

assert 0..4 == list.takeWhile { it < 5 }
assert 5..10 == list.dropWhile { it < 5 }


def m = [name: 'mrhaki', loves: 'Groovy', worksAt: 'JDriven']

assert [name: 'mrhaki'] == m.takeWhile { key, value -> key.length() == 4 }
assert [loves: 'Groovy', worksAt: 'JDriven'] == m.dropWhile { it.key == 'name' }

(Code is written with Groovy 2.0.4)

 
Element Groovy (programming language) Drops (app)

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
  • Observability for the Invisible: Tracing Message Drops in Kafka Pipelines
  • The Unreasonable Effectiveness of the Actor Model for Creating Agentic LLM Applications
  • Migrating from React Router v5 to v6: A Comprehensive Guide

Partner Resources

×

Comments

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

  • 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