Sunday 27 January 2019

Groovy: Get size of map


'size()' method returns number of <key, value> pairs in the map.

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

println "Total elements in the map are : ${countryCapitals.size()}"

println "\nAdding two more elements to the countryCapitals"

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

println "\nTotal elements in the map are : ${countryCapitals.size()}"

Output
Total elements in the map are : 2

Adding two more elements to the countryCapitals

Total elements in the map are : 4


Previous                                                 Next                                                 Home

No comments:

Post a Comment