Java Reference
In-Depth Information
[] .
Lines 2 to 4 create a list with an item already in it and add items to the list.
On line 1, an empty list is created by assigning a property the value of
Line 5 iterates over the list, invoking the closure to print out the contents. The
each() method provides the ability to iterate over all elements in the list, invoking
the closure on each element.
Lines 6 illustrates how to use an index to access a list. Lists are zero-based.
"Vishal" .
Line 7 shows how to use an index to assign position 0 the value
get() method.
Line 8 accesses the list using the
A
B
C
A
Vishal
Vishal
B
Maps
A Groovy map is an unordered collection of key-value pairs, where the key is unique, just as in
Java. It is an implementation of java.util.Map . Listing B-19 illustrates how to create maps and
common usages.
Listing B-19. Creating and Using Maps
1. def map = ['a':'Value A', 'b':'Value B ']
2. println map["a"]
3. println map."a"
4. println map.a
5. println map.getAt("b")
6. println map.get("b")
7. println map.get("c", "unknown")
8. println map
9. map.d = "Value D"
10. println map.d
11. map.put('e', 'Value E')
12. println map.e
13. map.putAt 'f', 'Value F'
14. println map.f
15. map.each { println "Key: ${it.key}, Value: ${it.value}" }
16. map.values().each { println it }
 
Search WWH ::




Custom Search