pairMap maps each adjacent pair of stream elements to a result by retaining the previous element. A typical implementation stores state inside map() and removes the first output with skip(1). section 9.5
Adjacent-element mapping
Given a, b, c, and d, mapping each pair with (left, right) -> left + "->" + right produces a->b, b->c, and c->d.
The implementation violates the Stream API contract because map() functions must be stateless. It assumes left-to-right processing: sequential streams may appear to work, although that order is not guaranteed, while parallel streams are completely broken. Calling sequential() inside pairMap() cannot prevent a client from later calling parallel(). section 9.5
Avoid the failure by following these rules:
- Test stream-consuming or stream-producing utilities after applying parallel(). section 9.5
- Use a correctly implemented third-party operation such as StreamEx when dependencies permit; otherwise prefer a conventional loop. section 9.5
