Posts tagged Groovy 1.8.1
Groovy Goodness: Use inject Method on a Map
Sep 27th
The inject() method is since Groovy 1.8.1 also available for Map objects. The closure arguments accepts two or three arguments. With the three-argument variant we get the key and value separately as arguments. Otherwise we get a map entry as closure argument.
Groovy Goodness: Use Connection Parameters to Get Text From URL
Sep 22nd
For a long time we can simply get the text from an URL in Groovy. Since Groovy 1.8.1 we can set parameters to the underlying URLConnection that is used to get the content. The parameters are passed as a Map to the getText() method or to the newReader() or newInputStream() methods for an URL.
Groovy Goodness: Streaming JSON with StreamingJsonBuilder
Sep 21st
Since Groovy 1.8 we can use JSONBuilder to create JSON data structures. With Groovy 1.8.1 we have a variant of JsonBuilder that will not create a data structure in memory, but will stream directly to a writer the JSON structure: StreamingJsonBuilder. This is useful in situations where we don’t have to change the structure and need a memory efficient way to write JSON.
Groovy Goodness: Collect on Nested Collections
Sep 19th
The collect() method has been around in Groovy for a long time and it is very useful. With the collect() method we can iterate through a collection and transform each element with a Closure to another value. To apply a transformation to collections in collections we can use the collectAll() method. Since Groovy 1.8.1 the collectAll() method is deprecated in favor of the new collectNested() method. So with collectNested() we can transform elements in a collection and even in nested collections and the result will be a collection (with nested collections) with transformed elements.
We can pass an initial collection to the method to which the transformed elements are added.
Groovy Goodness: GroupBy with Multiple Closures
Sep 16th
We can group elements in a List or Map with the groupBy() method for a long time in Groovy. We pass a closure with the grouping condition to get a Map with the items grouped. And since Groovy 1.8.1 we can use more than closure to do the grouping. We can use it for both List and Map objects.
Groovy Goodness: Sort or Remove Duplicates without Changing the Original Collection
Sep 15th
We can sort and remove duplicates from collections or arrays in Groovy for a long time. The sort() and unique() methods alter the original collection, so there is a big side effect. Since Groovy 1.8.1 we can use a boolean argument to indicate if we want to mutate the original collection or not. So now we can remove the side effect and leave the original collection or array unchanged.
Groovy Goodness: Take and Drop Items from a List
Sep 14th
When working with List object we get a lot of nice and useful methods we can use in Groovy. Since Groovy 1.8.1 we can use the methods take() and drop(). With the take() method we get items from the beginning of the List. We pass the number of items we want as an argument to the method.