July 2013
Intermediate to advanced
370 pages
8h 27m
English
Groovy has added quite a few convenience methods to maps.[26] We can iterate over a Map, just like how we iterated over an ArrayList (see Section 6.2, Iterating Over an ArrayList).
Map has a flavor of the
each
and
collect
methods.
Let’s look at an example of using the
each
method:
| WorkingWithCollections/NavigatingMap.groovy | |
| | langs = ['C++' : 'Stroustrup', 'Java' : 'Gosling', 'Lisp' : 'McCarthy'] |
| | |
| | langs.each { entry -> |
| | println "Language $entry.key was authored by $entry.value" |
| | } |
The output from the previous code is as follows:
| | Language C++ was authored by Stroustrup |
| | Language Java was authored by Gosling |
| | Language Lisp was authored by McCarthy |
If the closure we ...
Read now
Unlock full access