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 667 views
667 views asked Oct 4, 2020 by avibootz
1 answer 428 views
4 answers 856 views
2 answers 477 views
1 answer 362 views
1 answer 407 views
407 views asked Oct 5, 2020 by avibootz
1 answer 346 views
346 views asked Oct 5, 2020 by avibootz
...