title: Parallel streams, collectors and thread safety
body: <p>See the simple example below that counts the number of occurences of each word in a list:</p>
  
  <pre><code>Stream&lt;String&gt; words = Stream.of("a", "b", "a", "c");
  Map&lt;String, Integer&gt; wordsCount = words.collect(toMap(s -&gt; s, s -&gt; 1,
                                                        (i, j) -&gt; i + j));
  </code></pre>
  
  <p>At the end, <code>wordsCount</code> is <code>{a=2, b=1, c=1}</code>.</p>
  
  <p>But my stream is very large and I want to parallelise the job, so I write:</p>
  
  <pre><code>Map&lt;String, Integer&gt; wordsCount = words.parallel()
                                         .collect(toMap(s -&gt; s, s -&gt; 1,
                                                        (i, j) -&gt; i + j));
  </code></pre>
  
  <p>However I have noticed that <code>wordsCount</code> is a simple <code>HashMap</code> so I wonder if I need to explicitly ask for a concurrent map to ensure thread safety:</p>
  
  <pre><code>Map&lt;String, Integer&gt; wordsCount = words.parallel()
                                         .collect(toConcurrentMap(s -&gt; s, s -&gt; 1,
                                                                  (i, j) -&gt; i + j));
  </code></pre>
  
  <p><strong>Can non-concurrent collectors be safely used with a parallel stream or should I only use the concurrent versions when collecting from a parallel stream?</strong></p>