title: Groovy Script: List / Map Collection
body: <p>I've created a script which I'm using to simulate the behaviour of a SOAP service in SOAP UI (as a mock service) for sprint testing purposes but am having problems when trying to iterate over a List I've created. The List is made up of a number of Maps, and each Map contains a List. It should look something like this: </p>
  
  <pre><code>[[Rec: 1, Items: ["AB123","BC234","CD345"]],[Rec: 2, Items: ["AB123","BC234","CD345","DE456"]]]
  </code></pre>
  
  <p>And this is the code I have to build up the List: </p>
  
  <pre><code>def offerMap      = [:] 
  def outputList    = [] 
  def offerItemList = [] 
  
  def outputMap     = [:] 
  def outList       = [] 
  
  def item          = "" 
  def rec           = "" 
  
  offerItemList.add("AB123") 
  offerItemList.add("BC234") 
  offerItemList.add("CD345") 
  
  offerMap.put("Rec",1) 
  offerMap.put("Items",offerItemList) 
  
  outputList.add(offerMap) 
  
  log.info "OUT: outputList.size ${outputList.size()}" 
  log.info "OUT: offerItemList.size ${offerItemList.size()}" 
  
  offerMap.clear() 
  offerItemList.clear() 
  
  offerItemList.add("AB123") 
  offerItemList.add("BC234") 
  offerItemList.add("CD345") 
  offerItemList.add("DE456") 
  
  offerMap.put("Rec",2) 
  offerMap.put("Items",offerItemList) 
  
  outputList.add(offerMap) 
  
  log.info "OUT: outputList.size ${outputList.size()}" 
  log.info "OUT: offerItemList.size ${offerItemList.size()}" 
  </code></pre>
  
  <p>And this is the the code I have to iterate over the list: </p>
  
  <pre><code>outputList.each { 
  
      log.info "OUT: outputList.size ${outputList.size()}" 
  
      outputMap.clear() 
      outputMap = it 
  
      rec = outputMap.get("Rec") 
      log.info "OUT: REC ${rec}" 
  
      outList.clear() 
      outList = outputMap.get("Items") 
  
      outList.each { 
  
          item = it 
          log.info "OUT: Item ${item}" 
  
      } 
  }
  </code></pre>
  
  <p>But this is not giving me the results I expect. The first problem is that the <code>outputList.each</code> loop appears to immediately be jumping to the second entry in the list, as witnessed from the output: </p>
  
  <pre><code>Fri Nov 03 17:54:32 GMT 2017:INFO:OUT: outputList.size 1 
  Fri Nov 03 17:54:32 GMT 2017:INFO:OUT: offerItemList.size 3 
  Fri Nov 03 17:54:32 GMT 2017:INFO:OUT: outputList.size 2 
  Fri Nov 03 17:54:32 GMT 2017:INFO:OUT: offerItemList.size 4 
  Fri Nov 03 17:54:32 GMT 2017:INFO:OUT: outputList.size 2 
  Fri Nov 03 17:54:32 GMT 2017:INFO:OUT: REC 2 
  Fri Nov 03 17:54:32 GMT 2017:INFO:OUT: Item AB123 
  Fri Nov 03 17:54:32 GMT 2017:INFO:OUT: Item BC234 
  Fri Nov 03 17:54:32 GMT 2017:INFO:OUT: Item CD345 
  Fri Nov 03 17:54:32 GMT 2017:INFO:OUT: Item DE456 
  Fri Nov 03 17:54:32 GMT 2017:INFO:OUT: outputList.size 2 
  Fri Nov 03 17:54:32 GMT 2017:INFO:OUT: REC null 
  </code></pre>
  
  <p>I'm running out of ideas and fear I may be missing something fundamental due to my lack of experience with Groovy.</p>