Sunday 27 January 2019

Groovy: Get elements of a map


There are two ways to get the elements of a map.
a.   Using get method
b.   Using subscript operator.

HelloWorld.groovy
def countryCapitals = ["India" : "new Delhi", "Australia" : "Canberra"]

countryCapitals.put("Bulgaria", "Sofia")
countryCapitals["Canada"] = "Ottawa"

println "Capital of Bulgaria is ${countryCapitals.get('Bulgaria')}"
println "Capital of India ${countryCapitals['India']}"

Output
Capital of Bulgaria is Sofia
Capital of India new Delhi


Previous                                                 Next                                                 Home

No comments:

Post a Comment