Hi all, my name is Hubert A. Klein Ikkink. Not a very common name, right? To make things easier I just picked the first letters of my firstname and surname and came up with haki. So there you have it, now I am also known as Mr. Haki or mrhaki for short. You can read more blog postings at www.mrhaki.com. I am a passionate Groovy and Java developer based in Tilburg, The Netherlands. My goal is to write clean, elegant, user-centered and high quality software. You can find me on Google+ and Twitter. Hubert is a DZone MVB and is not an employee of DZone and has posted 173 posts at DZone. You can read more from them at their website. View Full User Profile

Groovy Goodness: Create a List with Default Values

  • submit to reddit

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)



Published at DZone with permission of Hubert Klein Ikkink, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)