The package summary of java.util.stream
states the following:
An example of a stateful lambda is the parameter to
map()
in:Set<Integer> seen = Collections.synchronizedSet(new HashSet<>()); stream.parallel().map(e -> { if (seen.add(e)) return 0; else return e; })...
Here, if the mapping operation is performed in parallel, the results for the same input could vary from run to run, due to thread scheduling differences, whereas, with a stateless lambda expression the results would always be the same.
I don't understand why this wouldn't produce consistent results, given that the set is synchronized and can only process one element at a time. Can you complete the above example in a way that demonstrates how the result could vary due to parallelization?