What is the order in which stream operations are applied to list elements?

Viewed 2491

Suppose we have a standard method chain of stream operations:

Arrays.asList("a", "bc", "def").stream()
  .filter(e -> e.length() != 2)
  .map(e -> e.length())
  .forEach(e -> System.out.println(e));

Are there any guarantees in the JLS regarding the order in which stream operations are applied to the list elements?

For example, is it guaranteed that:

  1. Applying the filter predicate to "bc" is not going to happen before applying the filter predicate to "a"?
  2. Applying the mapping function to "def" is not going to happen before applying the mapping function to "a"?
  3. 1 will be printed before 3?

Note: I am talking here specifically about stream(), not parallelStream() where it is expected that operations like mapping and filtering are done in parallel.

5 Answers
Related