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
No comments:
Post a Comment