I am creating snippets with takeWhile to explore its possibilities. When used in conjunction with flatMap, the behaviour is not in line with the expectation. Please find the cod
This is a bug in JDK 9 - from issue #8193856:
takeWhileis incorrectly assuming that an upstream operation supports and honors cancellation, which unfortunately is not the case forflatMap.
If the stream is ordered, takeWhile should show the expected behavior. This is not entirely the case in your code because you use forEach, which waives order. If you care about it, which you do in this example, you should use forEachOrdered instead. Funny thing: That doesn't change anything.