Sunday 27 January 2019

Groovy: Working with Maps


In Groovy, maps are created using map literal syntax notation [:].

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

println "Capital of India : ${countryCapitals.get('India')}"
println "Capital of Australia : ${countryCapitals.get('Australia')}"

Output
Capital of India : new Delhi
Capital of Australia : Canberra

By default, a map is an instance of 'java.util.LinkedHashMap'.


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

println "Class name : ${countryCapitals.getClass()}"

Output
Class name : class java.util.LinkedHashMap


Previous                                                 Next                                                 Home

No comments:

Post a Comment