2

How can I add mylist as keys and myanotherlist as values to myMap

def mylist = [ John, Paul, Emily ]
def myanotherlist = [ 23, 45, 56 ]

def myMap = [:]
//
  println "myMap: ${myMap}"
  println "Age of Paul is: ${myMap['Paul']}"

Desired output:

  myMap: [
    John : 23 
    Paul : 45
    Emily : 56
   ]

 Age of Paul is: 45
Jill448
  • 1,745
  • 10
  • 37
  • 62

2 Answers2

4

You can transpose the lists together (zip in other languages), then collect the entries

[mylist, myanotherlist].transpose().collectEntries()
tim_yates
  • 167,322
  • 27
  • 342
  • 338
2

You can use collectEntries on a list of indices (0 to mylist.size()):

def myMap = (0..<mylist.size()).collectEntries{[(mylist[it]) : myanotherlist[it]]}
ernest_k
  • 44,416
  • 5
  • 53
  • 99