title: java.io.NotSerializableException: java.util.ArrayList$Itr HashMap Jenkins Pipeline
body: <p>I have a groovy functions that converts a local variable into a map and allocates the variable to an environmental variable.</p>
  
  <pre><code>def variableMap() {
    def var = varList.toSpreadMap()
    env.varMap = "${var}"
  }
  </code></pre>
  
  <p>I use the environmental variable in the pipeline stages and iterate over it.</p>
  
  <pre><code>@Library('my_library') _
  pipeline {
   stages {
    mylibrary.variableMap()
  
    stage ('One') {
    steps {
     env.varMap.each { key,value -&gt;
                   echo "${key}"
                   echo "${value}"
                   ...
      }
    }
   }
  }
  </code></pre>
  
  <p>However, for some reason I get the following error:</p>
  
  <pre><code>Caused: java.io.NotSerializableException: java.util.ArrayList$Itr
      at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:926)
      at org.jboss.marshalling.river.BlockMarshaller.doWriteObject(BlockMarshaller.java:65)
      at org.jboss.marshalling.river.BlockMarshaller.writeObject(BlockMarshaller.java:56)
      at org.jboss.marshalling.MarshallerObjectOutputStream.writeObjectOverride(MarshallerObjectOutputStream.java:50)
      at org.jboss.marshalling.river.RiverObjectOutputStream.writeObjectOverride(RiverObjectOutputStream.java:179)
      at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:344)
      at java.util.HashMap.internalWriteEntries(HashMap.java:1793)
      at java.util.HashMap.writeObject(HashMap.java:1363)
      at sun.reflect.GeneratedMethodAccessor481.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)
      at org.jboss.marshalling.reflect.JDKSpecific$SerMethods.callWriteObject(JDKSpecific.java:156)
      at org.jboss.marshalling.reflect.SerializableClass.callWriteObject(SerializableClass.java:191)
      at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:1028)
      at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:920)
      at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1082)
      at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:1040)
      at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:920)
      at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1082)
      at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:1040)
      at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:1019)
      at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:920)
      at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1082)
      at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:1040)
      at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:1019)
      at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:1019)
      at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:920)
      at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1082)
      at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:1040)
      at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:1019)
      at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:920)
  </code></pre>
  
  <p>What is the reason for this issue? Is there a way to solve this?</p>
  
  <p>Note: 
  I found a similar post. But I am not sure how the workaround can help me in this case. </p>
  
  <p><a href="https://stackoverflow.com/questions/31654497/how-to-fix-notserializableexception-error-during-jenkins-workflow-build">How to fix NotSerializableException error during Jenkins workflow build?</a></p>
  
  <p>Also I cannot understand the explanation behind the issue.</p>
  
  <p><strong>Edit</strong></p>
  
  <p>Also worth mentioning that I annotated the method as some solutions online suggest. But the result was the same:</p>
  
  <pre><code>@NonCPS
  def variableMap() {
    def var = varList.toSpreadMap()
    env.varMap = "${var}"
  }
  </code></pre>