It will move throughout the thread, sending tasks to the fork-join pool, breaking the list into parts and passing all the list items to this empty lambda. It is currently not possible to check at runtime whether a lambda expression is empty or not, so it cannot be optimized.
A similar problem occurs when using Collector . All collectors have a finisher operation, but in many cases it is an identity function of type x -> x . In this case, sometimes the code that uses the collectors can be highly optimized, but you cannot determine exactly if the provided lambda is an identity or not. To solve this problem, an additional collector characteristic called IDENTITY_FINISH was introduced instead. If it were possible to accurately determine whether the lambda provided by the identification function, this characteristic is not needed.
Also review the JDK-8067971 discussion. This involves creating static constants, such as Predicate.TRUE (always true) or Predicate.FALSE (always false) to optimize operations like Stream.filter . For example, if Predicate.TRUE supplied, then the filtering step can be deleted, and if Predicate.FALSE is provided, then the stream can be replaced by an empty stream at this point. Again, one could discover at runtime that the supplied predicate is always right, then it would not be necessary to create such constants.
source share