Java Reference
In-Depth Information
Thefirstexpression states that there'satleast onecity whosename islessthan7characters.
The second expression says that all of the city names are 10 characters or less.
Table B.1 summarizes the searchable methods.
Table B.1. Searchable methods added to Groovy collections
Method
Description
any
Returns true if any element satisfies closure
every
Returns true if all elements satisfy closure
find
Returns first element satisfying closure
findAll
Returns list of all elements satisfying closure
Finally, the join method concatenates all the elements of the list into a single string, using
the supplied separator:
assert cities. join (',') == "Boston,Seattle,New York,Cleveland"
The combination of native syntax and added convenience methods makes Groovy lists
much easier to work with than their Java counterparts. As it turns out, maps are improved
the same way.
B.4.3. Maps
GroovymapsarelikeJavamaps,butagainwithanativesyntaxandadditionalhelpermeth-
ods. Groovy uses the same square-bracket syntax for maps as for lists, but each entry in the
map uses a colon to separate the key from its corresponding value.
Youcanpopulateamaprightawaybyaddingtheelementswhenyoudeclarethemapitself:
def trivialMap = [x:1, y:2, z:3]
assert 1 == trivialMap['x']
assert trivialMap instanceof java.util.HashMap
This defines a map with three entries. When adding elements to the map, the keys are as-
sumed to be strings, so you don't need to put quotes around them. The values can be any-
thing.
 
Search WWH ::




Custom Search