The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

Shay8 (2) [Avatar] Offline

Oracle's API for Stream writes: “To preserve correct behavior, these behavioral parameters must be non-interfering (they do not modify the stream source); and in most cases must be stateless (their result should not depend on any state that might change during execution of the stream pipeline).”

But PrimeNumbersCollector.accumulator() is stateful, because its result depends on a state being changed during the execution of the stream – its first parameter, the result container Map<Boolean, List<Integer>> acc.
I don't think that this should be a problem for sequential stream, but it is very likely to be a problem for parallel streams.

Although the book says on page 154 in 6.6.1 “Note that in reality this collector can’t be used in parallel, because the algorithm is inherently sequential”, it actually can, if the stream is parallel (as I wrote in my previous post). So executing it in parallel streams might return wrong results.

For these reasons I think that while this algorithm is interesting to study, but not as an example for a Collector; it is not a good case study in this context. Because the algorithm is inherently sequential and stateful, I think it is better to implement it by a simple sequential loop, instead of in the context of Stream.

Also, in a comment for the code it says: “This collector is IDENTITY_FINISH but neither UNORDERED nor CONCURRENT because it relies on the fact that prime numbers are discovered in sequence”. It is, indeed, not CONCURRENT, but not for the reasons stated. Instead, it is because the Map supplied by the supplier() is java.util.HashMap.