Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

9
  • 82
    Why doesn't it make sense? Since a stream is a pipeline there's no reason it couldn't create two producers of the original stream, I could see this being handled by a collector that provides two streams. Commented Feb 12, 2014 at 7:24
  • 39
    Not thread safe. Bad advice trying to add directly to a collection, that is why we have the stream.collect(...) for with predefined thread-safe Collectors, that work well even on non-thread-safe Collections (with no synchronized lock contention). Best answer by @MarkJeronimus. Commented May 8, 2015 at 4:04
  • 1
    @JoD It is thread-safe if heads and tails are thread-safe. Additionally, assuming the use of non-parallel streams, only the order is not guaranteed, so they are thread-safe. It's up to the programmer to fix concurrency issues, so this answer is perfectly suitable if the collections are thread safe. Commented Feb 15, 2016 at 23:55
  • 1
    @Nixon it is not suitable in the presence of a better solution, which we have here. Having such code can lead to bad precedent, causing others to use it in a wrong way. Even if no parallel streams are used, it is only one step away. Good coding practices require us not to maintain state during stream operations. Next thing we do is coding in a framework like Apache spark, and same practices would really lead to unexpected results. It was a creative solution, I give that, one I might have written myself not so long ago. Commented Feb 16, 2016 at 0:25
  • 1
    @JoD It is not a better solution, it's factually more inefficient.That line of thinking ultimately ends up with the conclusion that all Collections should be thread safe by default to prevent unintended consequences, which is simply wrong. Commented Feb 29, 2016 at 20:50