Java流peek()操作

huangapple go评论73阅读模式
英文:

Java stream peek() operation

问题

我一直在尝试使用流操作,并试图理解为什么以下代码不会将每个整数转换为字符串。我对peek()的理解是它充当中间操作符,如果后面跟着一个终端操作符,则将给定操作应用于流。任何帮助都会很棒!

List<Integer> testList = Arrays.asList(10, 11, 12, 13, 14, 15);
testList.stream().peek(x -> x.toString()).forEach(x -> System.out.println(x.getClass()));
英文:

I've been trying to play around with stream operations, and am trying to understand why the following doesn't convert each integer to a string. My understanding of peek() is that it acts as an intermediate operator, and applies the given operation to the stream if it is followed by a terminal operator. Any help would be great!

List&lt;Integer&gt; testList = Arrays.asList(10, 11, 12, 13, 14, 15);
testList.stream().peek(x -&gt; x.toString()).forEach(x -&gt; System.out.println(x.getClass()));

答案1

得分: 3

peek 对每个流元素执行操作,但不修改流。它通常用于在调试期间在某些操作之前或之后打印 Stream 的元素,例如 stream.peek(System.out::println)peek 的文档 表明:

返回一个由此流的元素组成的流,另外对从生成的流中消耗的每个元素执行提供的操作。

你正在寻找 Stream#map,它会在调用该函数时将 Stream 的每个元素转换为函数的结果。根据文档

返回一个由将给定函数应用于此流的元素的结果组成的流。

英文:

peek performs the operation on each stream element, but does not modify the stream. It is often used to print the elements of a Stream before or after some operations for debugging, e.g. stream.peek(System.out::println). The documentation for peek states that it:

> 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.

You are looking for Stream#map, which converts each element of the Stream to the result of the function when called with the element. According to the documentation, it:

> Returns a stream consisting of the results of applying the given function to the elements of this stream.

答案2

得分: 0

peek运算符不对流进行任何更改。它只允许您查看流经管道的项目,就像它是一个窗口一样。您不能转换项目,也不能过滤它们,或以其他方式修改流 - 还有其他运算符可以做到这一点(例如mapfilter)。

您可以在这里找到有关Stream.peek的更长讨论:https://www.baeldung.com/java-streams-peek-api

英文:

The peek operator makes no changes to the stream. It only allows you to look at the items that are flowing through the pipeline in the location where you put it, as if it was a window. You cannot transform the items, or filter them, or otherwise modify the stream - there are other operators for that (such as map and filter).

You can find a longer discussion on Stream.peek here: https://www.baeldung.com/java-streams-peek-api

答案3

得分: 0

这个方法 peek 接受一个 consumer 作为参数,而 consumer 只是消耗它所接受的元素,它接受一个值作为参数并返回一个 void,这意味着这里返回的值 x -&gt; x.toString() 在 JRE 中会被忽略,而 peek 通常用于调试,peek 的意思是看但不触摸。你应该使用 map 而不是 peek。

英文:

The method peek take a consumer as argument, and consumer just consumes elements it take a value as argument and returns a void, which means that the value returned here x -&gt; x.toString() is juste ignored by the JRE, also peek is destinated for debugging, peek means look but don't touch. You want to use map instead.

huangapple
  • 本文由 发表于 2020年8月10日 05:33:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/63331553.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定