Sunday, 27 January 2019

Maps in Groovy are heterogeneous


Maps in Groovy are heterogeneous, keys and values can be of any type.

HelloWorld.groovy
class Employee{
 public name
 public id
}

Employee emp1 = new Employee()
emp1.name = "Krishna"
emp1.id = 29

def map = [
 simpleType : 123,
 complexType : [a : 1, b : 2],
 boolType : true,
 (emp1) : "ABC Corporation"
]

map.each { entry ->
    println "key: $entry.key value: $entry.value, keyClass : ${entry.key.getClass()}, valueClass : ${entry.value.getClass()}"
}

Output
key: simpleType value: 123, keyClass : class java.lang.String, valueClass : class java.lang.Integer
key: complexType value: [a:1, b:2], keyClass : class java.lang.String, valueClass : class java.util.LinkedHashMap
key: boolType value: true, keyClass : class java.lang.String, valueClass : class java.lang.Boolean
key: Employee@f37659 value: ABC Corporation, keyClass : class Employee, valueClass : class java.lang.String



Previous                                                 Next                                                 Home

No comments:

Post a Comment