In my previous blog i talked about various operators and control structures available in Groovy. Today i am going to talk about Groovy collections. Like Java, we have List and Map in Groovy. Groovy also has something called Range.
List is an ordered collection of objects. Groovy lists are indexed using [] operator. We can use [] to define an empty list and << to add items to a list.
Following code snippet shows how a list is defined in Groovy.
List Methods
Lists support many useful methods. Following code snippet illustrate the usage of some of the list methods.
Maps
Map is a collection of key value pair. We can use [:] to define an empty map. If the keys in a Map is String, it can act as a bean.
Following code snippet shows how to define and use Maps in Groovy.
Map Methods
Maps support many useful methods. Following code snippet illustrate the usage of some of the Map methods.
Range
Range can be used to specify a list of sequential values. Range is defined using the first and last value of the sequence. Range can also be defined to exclude the last value. Use .. operator to include first and last value. Use ..< operator to exclude last value.
Ranges can be used for any Java object which implements java.lang.Comparable for comparison and also have methods next () and previous() to return the next / previous item in the range.
follwoing code snippet shows how to define and use Range in Groovy.
Range Methods
Like Lists and Maps, Range also support many useful methods. Following code snippet illustrate the usage of some of the Range methods.
No comments:
Post a Comment