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 Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
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
Join us tomorrow at 1 PM EST: "3-Step Approach to Comprehensive Runtime Application Security"
Save your seat
  1. DZone
  2. Coding
  3. Languages
  4. Groovy Goodness: Create a List with Default Values

Groovy Goodness: Create a List with Default Values

Hubert Klein Ikkink user avatar by
Hubert Klein Ikkink
·
Oct. 18, 12 · Interview
Like (0)
Save
Tweet
Share
5.37K Views

Join the DZone community and get the full member experience.

Join For Free

Since Groovy 1.8.7 we can create a list and use the withDefault() method to define a default value for elements that are not yet in the list. We use a closure as argument of the method, which returns the default value. We can even access the index of the element in the closure as an argument.

Besides the withDefault() method we can use the withLazyDefault() which is just another name for the same functionality. If we request a value for an index that is greater or equal to the size of the list, the list will automatically grow up to the specified index. Any gaps are filled with the value null.

We can also use the withEagerDefault() method. If we use this method the gaps are filled with the result from the closure we pass to the method. So instead of the value null the return result from the closure is used.

The following example script shows the results from using the withDefault() (withLazyDefault()) and withEagerDefault():

def lazy = ['abc', 42].withDefault { 'default' } // Or .withLazyDefault {}

// List grows because 3 is bigger than
// current lazy list size. Value is
// for element at index 3 is set with value
// from init closure.
assert lazy[3] == 'default'

// Gap at index 2 is filled with null value.
assert lazy == ['abc', 42, null, 'default']

// Because value at index 2 is set to null
// before, the init closure is invoked
// for this index now.
assert lazy[2] == 'default'

// List is now filled.
assert lazy == ['abc', 42, 'default', 'default']


def eager = ['abc', 42].withEagerDefault { 'default' }

// Here the list also grows, because
// index 3 is bigger than eager list size.
assert eager[3] == 'default'

// The gaps in the list are now
// filled with the value from
// the init closure.
assert eager == ['abc', 42, 'default', 'default']

// Value is already set because we
// used withEagerDefault.
assert eager[2] == 'default'


// We can use the index value as argument
// for the init closure.
def sample = [1,2,3].withDefault { index ->
    index % 2
}

assert sample[3] == 1
assert sample[4] == 0

(Written with Groovy 2.0.5)

 

 

Groovy (programming language)

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Explaining: MVP vs. PoC vs. Prototype
  • When AI Strengthens Good Old Chatbots: A Brief History of Conversational AI
  • Playwright vs. Cypress: The King Is Dead, Long Live the King?
  • Easy Smart Contract Debugging With Truffle’s Console.log

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: