Learn Java for Web Development

(Tina Meador) #1

412 APPENDIX B: Introduction to Groovy


   Line 1 illustrates how to define a map with multiple entries. When using the
square bracket notation, the colon separates the key from the value.
 Lines 2 to 10 show several different techniques for accessing the map.
 Lines 11 to 13 show several different techniques for putting items into the map.

Value A


Value A


Value A


Value B


Value B


unknown


[a:Value A, b:Value B , c:unknown]


Value D


Value E


Value F


Key: a, Value: Value A


Key: b, Value: Value B


Key: c, Value: unknown


Key: d, Value: Value D


Key: e, Value: Value E


Key: f, Value: Value F


Value A


Value B


unknown


Value D


Value E


Value F


Ranges

A range is a list of sequential values. Logically, you can think of it as 1 through 10 or as a through z.
As a matter of fact, the declaration of a range is exactly that: 1..10 or 'a'.'z'. A range is a list of
any objects that implement java.lang.Comparable. The objects have next() and previous()
methods to facilitate navigating through the range. Listing B-20 illustrates some of the things you
can do with ranges.

Free download pdf