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 555 views
555 views asked Oct 4, 2020 by avibootz
1 answer 304 views
4 answers 667 views
2 answers 311 views
1 answer 263 views
1 answer 307 views
307 views asked Oct 5, 2020 by avibootz
1 answer 255 views
255 views asked Oct 5, 2020 by avibootz
...