Sunday, 27 January 2019

Groovy: Insert elements into a map


There are two ways to insert elements into a map.
a.   Using put method
b.   Using subscript operator.

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

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

countryCapitals.each { entry ->
    println "CountryName: $entry.key, Capital: $entry.value"
}

Output
CountryName: India, Capital: new Delhi
CountryName: Australia, Capital: Canberra
CountryName: Bulgaria, Capital: Sofia
CountryName: Canada, Capital: Ottawa


Previous                                                 Next                                                 Home

No comments:

Post a Comment