Sunday 27 January 2019

Groovy: Remove all the elements from the map


'clear' method is used to remove all the elements from the map.

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

printMap(countryCapitals)

println "\nClearing all the elements of the map\n"

countryCapitals.clear()

printMap(countryCapitals)

void printMap(map){
 println "Size : ${map.size()}"
 
 map.eachWithIndex {entry, i -> 
  println "$i : CountryName: $entry.key, Capital: $entry.value"
 }
}

Output
Size : 2
0 : CountryName: India, Capital: new Delhi
1 : CountryName: Australia, Capital: Canberra

Clearing all the elements of the map

Size : 0



Previous                                                 Next                                                 Home

No comments:

Post a Comment