List
Groovy allows to initialize List via square brackets:
List<Integer> list = [1,2,3,4]
println list
println list.size() [1, 2, 3, 4] 4
Using def instead of actual type:
def list = [1,2,3,4]
println list
println list.size()
println list.getClass() [1, 2, 3, 4] 4 class java.util.ArrayList
Using no types:
list = [1,2,3,4]
println list
println list.size()
println list.getClass() [1, 2, 3, 4] 4 class java.util.ArrayList
As seen above Groovy creates ArrayList by default. We can also create a collection of a different type by specifying a type or by using as operator.
//using as operator
def list = [1,2,3,4] as LinkedList
println list
println list.getClass()
def aSet = [1,2,3,4] as HashSet
println aSet
println aSet.getClass()
//using explicit type
TreeSet aTreeSet = [3,2,4,1]
println aTreeSet
println aTreeSet.getClass() [1, 2, 3, 4] class java.util.LinkedList [1, 2, 3, 4] class java.util.HashSet [1, 2, 3, 4] class java.util.TreeSet
The
as keyword is used for type coercion i.e. auto type conversion by the compiler. Whereas, casting is an explicit conversion to be performed during runtime.
Using subscript operator [] for collections:
Similar to array, we can read/write the elements via [] subscript operator:
def aSet = [1,2,3,4]
println aSet[0]
println aSet[1]
println aSet[aSet.size() -1]
aSet[0] = 5
println aSet 1 2 4 [5, 2, 3, 4]
We can also use [] with negative indexes to read/write elements at the end.
def aSet = [1,2,3,4]
println aSet[-1]
aSet[-2] = 5
println aSet 4 [1, 2, 5, 4]
Using left shift operator << to append elements:
The leftShift operator << can be used to append elements to a list:
def fruits = ["apple", "banana"]
fruits << "orange"
println fruits [apple, banana, orange]
List of lists
We can also use nested [] to create list of lists:
def lists = [[1, 2], [10, 20]]
println lists
[[1, 2], [10, 20]]
Arrays
Groovy does not allow to declare and initialize an array with curly brackets (curly brackets are used for closures)
int[] num = {1,2,3,4} org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: C:\Users\Joe\AppData\Local\Temp\groovyTemp8813316133253589526.groovy: 1: unexpected token: 1 @ line 1, column 14. int[] num = {1,2,3,4} ^
1 error
Instead we need to use square brackets:
int[] num = [1,2,3,4]
print num; [1, 2, 3, 4]
The square brackets are also used for List (last section), so we must declare the type (like above example). Otherwise Groovy will create list instead of array:
num = [1,2,3,4] //no type
println num
println num.getClass()
println num.getClass().getComponentType()
def num2 = [1,2,3,4] //using def
println num2
println num2.getClass()
println num2.getClass().getComponentType() [1, 2, 3, 4] class java.util.ArrayList null [1, 2, 3, 4] class java.util.ArrayList null
We can also use as operator:
def arr = [1,2,3] as int[];
println arr
println arr.getClass().isArray()
println arr.getClass().getComponentType() [1, 2, 3] true int
Multi-dimensional array
def arr = [[1,2],[10,20]] as long[][]
println arr
println arr.getClass().isArray()
println arr.getClass().getComponentType()
println arr.getClass().getComponentType().getComponentType() [[1, 2], [10, 20]] true class [J long
Maps
Groovy also allows to declare maps within square brackets, where keys and values are separated by colons, and each key/value pairs are separated by commas.
nums = [one: 1, two: 2, three: 3]
println nums.getClass()
println nums
//other map
nums2 = [1: 'one', 2: 'two', 3: 'three']
println nums2.getClass()
println nums2
//another one
def food = [breakfast: "egg", lunch: "sushi", dinner: "pasta" ]
println food.getClass()
println food class java.util.LinkedHashMap [one:1, two:2, three:3] class java.util.LinkedHashMap [1:one, 2:two, 3:three] class java.util.LinkedHashMap [breakfast:egg, lunch:sushi, dinner:pasta]
As seen above, by default, groovy creates an instance of LinkedHashMap . To create a different type of map, we can declare the type explicitly or we can use as operator:
//explicit type
TreeMap nums = [one: 1, two: 2, three: 3]
println nums.getClass()
println nums
//as operator
def food = [breakfast: "egg", lunch: "sushi", dinner: "rice" ] as TreeMap
println food.getClass()
println food class java.util.TreeMap [one:1, three:3, two:2] class java.util.TreeMap [breakfast:egg, dinner:rice, lunch:sushi]
To read/write a map we can use subscript operator []
HashMap nums = [one: 1, two: 2, three: 3]
println nums['one'] //reading
nums['four'] = 4 //writing
println nums 1 [one:1, two:2, three:3, four:4]
Using variables in declaration
When we need to pass variable values as keys in the map definitions, we must surround the variable or expression with parentheses:
def key1= 'one'
def value2= 2;
def nums = [(key1): 1, two: value2]
println nums
def key= 1
def str= "wo"
def nums2 = [(key): "one", (key+1): "t${str}"]
println nums2
[one:1, two:2] [1:one, 2:two]
|
|