Java Reference
In-Depth Information
Map keys
When adding to a map, the keys are assumed to be of type
string
, so no quotes are ne-
cessary.
You can add to a map using either Java or Groovy syntax:
ALEast.put('Boston','Red Sox')
assert 'Red Sox' == ALEast.get('Boston')
assert ALEast == [Boston:'Red Sox']
ALEast['New York'] = 'Yankees'
10
For non-baseball people, ALEast is short for the Eastern division of the American League.
Accessing values can be done with either the array-like syntax shown, or using a dot. If the
key has spaces in it, wrap the key in quotes:
assert 'Red Sox' == ALEast.Boston
assert 'Yankees' == ALEast.'New York'
I've been using
def
to define the map reference, but Groovy understands Java generics:
Map<String,String> ALCentral = [Cleveland:'Indians',
Chicago:'White Sox',Detroit:'Tigers']
assert 3 == ALCentral.size()
assert ALCentral.Cleveland == 'Indians'
Maps have a
size
method that returns the number of entries. Actually, the
size
method
is universal.
Size
In Groovy, the
size
method works for arrays, lists, maps, strings, and more.
Maps have an overloaded
plus
operation that combines the entries from two maps: