? 众所周知在Java中使用Stream能够很好地帮我们流处理对象。而Stream中有一个peek方法,它与map最大的区别是它没有返回值。

? ? 一开始我是简单地把它当做一个void类型的处理方法去使用的,但是后来却发现程序执行到此处时,不进peek方法,也就是说peek根本就没有被执行。

? ? 后来翻看peek源码,发现里面是这样写的:?

? ??

Returns a stream consisting of the elements of this stream, additionally
performing the provided action on each element as elements are consumed
from the resulting stream.

This is an intermediate operation.

大意是说,仅在对流内元素进行操作时,peek才会被调用,当不对元素做任何操作时,peek自然也不会被调用了 - -...

最后,用foreach改写了那段代码,问题解决。

PS:其实peek源代码下还备注了一行:

This method exists mainly to support debugging, where you want to see the elements as they flow past a certain point in a pipeline.

大意是说该方法主要用于调试,方便debug查看Stream内进行处理的每个元素。

好嘛,我承认是我眼瞎了...

最近更新了idea的版本,发现idea居然都有贴心的提示了!


摸鱼癌晚期患者