Java Reference
In-Depth Information
In Groovy, if the last argument to any method is a closure you can put the closure outside
the parentheses. In this case the only argument to collect is a closure, so even the optional
parentheses are omitted.
What is a closure
A closure is a block of code, delimited by curly braces, which can be treated as an object.
The arrow notation is used to specify dummy arguments. In the closure applied to the map
inthe current example, the twodummy arguments are k and v ,which represent the keyand
value of each entry. The expression on the right side of the arrow says to substitute each
key and value into a GString separated by an equals sign. This collect method takes
each entry in the map and converts it into a string with the key assigned to the value, and
produces a list of results.
The result of the operation is shown here:
["cht=p3", "chs=250x100", "chd=t:60,40", "chl=Hello|World"]
This process is illustrated in figure 2.2 .
Figure 2.2. Apply collect to a map to convert it into a list, where each entry is transformed into a string.
To create the query string, use another method added by Groovy to collections, called
join . The join method takes a single argument that's used as the separator when assem-
 
Search WWH ::




Custom Search