July 2013
Intermediate to advanced
370 pages
8h 27m
English
Lists
Groovy adds a number of convenience methods to Collections. (For a list, refer to
http://groovy.codehaus.org/groovy-jdk/java/util/Collection.html
.) Let’s implement an example using the method we’re already familiar with—
each
. Then we’ll refactor that example using methods that will make our code self-contained and expressive. Along the way, we’ll see how Groovy treats code blocks as first-class citizens,
like functional programming languages do.
Suppose we have a collection of strings and want to count the total number of characters. Here’s a way to do that
using the
each
method:
| WorkingWithCollections/CollectionsConvenienceMethods.groovy | |
| | lst = ['Programming', 'In', 'Groovy'] |
| | |
| | |
Read now
Unlock full access