In which cases Stream operations should be stateful?

前端 未结 4 787
轮回少年
轮回少年 2021-01-01 03:30

In the javaodoc for the stream package, at the end of the section Parallelism, I read:

Most stream operations accept parameters that des

4条回答
  •  既然无缘
    2021-01-01 04:29

    When in doubt simply check the documentation to the specific operation. Examples:

    1. Stream.map mapper parameter:

      mapper - a non-interfering, stateless function to apply to each element

      Here documentation explicitly says that the function must be stateless.

    2. Stream.forEach action parameter:

      action - a non-interfering action to perform on the elements

      Here it's not specified that the action is stateless, thus it can be stateful.

    In general it's always explicitly written on every method documentation.

提交回复
热议问题