See the simple example below that counts the number of occurences of each word in a list:
Stream<String> words = Stream.of("a", "b", "a", "c");
Map<String, Integer> wordsCount = words.collect(toMap(s -> s, s -> 1,
(i, j) -> i + j));
At the end, wordsCount
is {a=2, b=1, c=1}
.
But my stream is very large and I want to parallelise the job, so I write:
Map<String, Integer> wordsCount = words.parallel()
.collect(toMap(s -> s, s -> 1,
(i, j) -> i + j));
However I have noticed that wordsCount
is a simple HashMap
so I wonder if I need to explicitly ask for a concurrent map to ensure thread safety:
Map<String, Integer> wordsCount = words.parallel()
.collect(toConcurrentMap(s -> s, s -> 1,
(i, j) -> i + j));
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?