How to add key and value to map in Groovy

2 Answers

0 votes
def map = [name: "Groovy", age: 13, country: "United States"]

map["key"] = "value"

map.each{key, value -> println "${key}:${value}"}

 
 
 
/*
run:
 
name:Groovy
age:13
country:United States
key:value
 
*/

 



answered Oct 4, 2020 by avibootz
0 votes
def map = [name: "Groovy", age: 13, country: "United States", city: "New York"]

map.put("key", "value")

println(map)
 

   
   
/*
run:
   
[name:Groovy, age:13, country:United States, city:New York, key:value]
   
*/

 



answered Oct 5, 2020 by avibootz

Related questions

1 answer 301 views
1 answer 261 views
1 answer 263 views
4 answers 665 views
2 answers 310 views
1 answer 168 views
1 answer 301 views
...