How to print map with index in Groovy

2 Answers

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

map.eachWithIndex{element, i -> println "$i $element.key: $element.value"}

 
 
 
/*
run:
 
0 name: Groovy
1 age: 13
2 country: United States
3 key: value
 
*/

 



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

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

 
 
 
/*
run:
 
0 name: Groovy
1 age: 13
2 country: United States
3 key: value
 
*/

 



answered Oct 4, 2020 by avibootz

Related questions

2 answers 545 views
545 views asked Oct 4, 2020 by avibootz
1 answer 286 views
4 answers 653 views
2 answers 293 views
1 answer 251 views
1 answer 298 views
298 views asked Oct 5, 2020 by avibootz
1 answer 247 views
247 views asked Oct 5, 2020 by avibootz
...