Groovy Goodness: Create a List with Default Values
Join the DZone community and get the full member experience.
Join For FreeSince 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, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
Writing a Vector Database in a Week in Rust
-
Building the World's Most Resilient To-Do List Application With Node.js, K8s, and Distributed SQL
-
Database Integration Tests With Spring Boot and Testcontainers
-
How Web3 Is Driving Social and Financial Empowerment
Comments