-
Notifications
You must be signed in to change notification settings - Fork 0
Collections
Philip Ford edited this page Aug 26, 2017
·
12 revisions
[item1, item2, ...]- The list items are separated by commas within brackets.
- To create an empty list, assign empty brackets to a reference.
[key1: value1, key2: value2, ...]- Syntax: a comma-separate list of key/value pairs in brackets.
- Each key is separated from its value by a colon.
- To create an empty map, assign brackets containing only a colon,
[:], to a reference. - There are at least 5 ways to add an item to a map, including bracket notation (e.g.,
map['item1'] = 'Hi!'), shown below.
You can add values to or retrieve values from a Groovy map with bracket notation, just like JavaScript objects:
// Here is an example map declaration with initial values
def personAgeMap = [matthew: 30, mark:25, luke: 40]
// Since Groovy is just a superset of Java, we can use the put method to add an entry to a map.
personAgeMap.put('john', 41)
// We can also use bracket notation. For example:
personAgeMap['Tim'] = 28
// Another way is to treat the key as property of the map object. Example:
personAgeMap.Roy = 32
// You can also use dot (.) followed by the key enclosed in quotes. Example:
personAgeMap.'Tim' = 55
// Lastly, you can add entries coming from another map. You can push entries from one map to another, for example:
personAgeMap << [Michael:29]