6.4 Other Convenience Methods on List
s
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'] |
| |
|
Get Programming Groovy 2 now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.