Search code examples
javajava-streamstatelessstateful

Do limit and skip operations become stateless when unordered


My question is quite simple. The intermediate limit and skip operations on java streams are lablled as being stateful operations - I assume because they by default need to limit or skip the first n elements on an ordered stream.

If I make the input source stream unordered by invoking the unordered() method or using an unordered source, can we effectively say that these operations could then be considered stateless, or am I missing something here?


Solution

  • No, limit and skip are still stateful operations as the processing of elements depends on information about the processing of other elements (i.e. whether they have been processed).

    These operations are easier to implement for unordered stream, but this doesn’t change their stateful nature.

    You can tell this by simply asking youself: “can this operation get implemented for a single element by just looking at the element and nothing else (besides unchanging information known prior to starting the entire stream operation)?”