title: Parallel Stream adding items to same array
body: <p>I have an array of Objects which I stream to use a function on each object to modify it and add it to a completely new array of Objects.</p>
  
  <p>By using <code>.parallel()</code> the execution time speeds up 2 times, but I'm getting some <code>NullPointerException</code>s when looping over the new array.</p>
  
  <p>I tried to debug it a few times without any success. It seems that this problem only occurs during the runtime. I tried implementing a synchronized function for adding the new objects to the list, but sadly it didn't work as well.</p>
  
  <p>Can anyone give me a suggestion how to get this working? Thanks in advance!</p>
  
  <p>Here is the code snippet:</p>
  
  <pre><code>private static final Object sync = new Object();
  private ArrayList&lt;Object&gt; newList = new ArrayList&lt;Object&gt;();      
  
  private void addNewObject(Object newObject) {
          synchronized (sync) {
              newList.add(newObject);
          }
      }
  
  private Object mutateObject(Object oldObject) {
      // Do something with the object here
      return mutatedObject;
  }
  
  public ArrayList&lt;Object&gt; createNewList(ArrayList&lt;Object&gt; oldList) {
      oldList.stream().parallel().forEach(object -&gt; addNewObject(mutateObject(object)));
      return newList;
  }
  </code></pre>