A stateful mapping operation retains the previous stream element while map() processes the current one. This violates the Stream API requirement that mapping functions be stateless. section 9.5
Adjacent-pair mapping
A pairMap utility stores prev, applies a BiFunction to prev and the current element, emits null for the first element, and removes it with skip(1). For "a", "b", "c", and "d", it aims to produce "a->b", "b->c", and "c->d". The assumption of left-to-right processing may work sequentially but breaks under parallel execution. section 9.5
Avoid the mistake by:
- Following the Stream API contract instead of adding state to map() functions. section 9.5
- Avoiding custom pairwise operations unless their sequential and parallel behavior is correctly implemented. section 9.5
- Considering the StreamEx implementation when a dependable pairwise operation is required. section 9.5
