takeWhile() working differently with flatmap

前端 未结 4 428
被撕碎了的回忆
被撕碎了的回忆 2021-01-30 12:18

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

4条回答
  •  既然无缘
    2021-01-30 12:51

    This is a bug in JDK 9 - from issue #8193856:

    takeWhile is incorrectly assuming that an upstream operation supports and honors cancellation, which unfortunately is not the case for flatMap.

    Explanation

    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.

提交回复
热议问题