title: Does Stream.forEach() always work in parallel?
body: <p>In <a href="http://www.ibm.com/developerworks/library/j-java-streams-2-brian-goetz/index.html" rel="noreferrer">Aggregating with Streams</a>, Brian Goetz compares populating a collection using Stream.collect() and doing the same using Stream.forEach(), with the following two snippets:</p>
  
  <pre><code>Set&lt;String&gt; uniqueStrings = strings.stream()
                                     .collect(HashSet::new,
                                              HashSet::add,
                                              HashSet::addAll);
  </code></pre>
  
  <p>And,</p>
  
  <pre><code>Set&lt;String&gt; set = new HashSet&lt;&gt;();
  strings.stream().forEach(s -&gt; set.add(s));
  </code></pre>
  
  <p>Then he explains:</p>
  
  <blockquote>
    <p>The key
    difference is that, with the forEach() version, multiple threads are trying to access a single result
    container simultaneously, whereas with parallel collect(), each thread has its own local result
    container, the results of which are merged afterward.</p>
  </blockquote>
  
  <p>To my understanding, multiple threads would be working in the forEach() case only if the stream is parallel.  However, in the example given, forEach() is operating on a sequential stream (no call to parallelStream()).</p>
  
  <p>So, is it that forEach() always work in parallel, or that the code snippet should call parallelStream() instead of stream().  (or that I'm missing something?)</p>