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 663 views
663 views asked Oct 4, 2020 by avibootz
1 answer 427 views
4 answers 853 views
2 answers 474 views
1 answer 361 views
1 answer 406 views
406 views asked Oct 5, 2020 by avibootz
1 answer 344 views
344 views asked Oct 5, 2020 by avibootz
...