![]() ![]() This gets clarified in the early draft of JDK 9’s docs eventually: The problem is that this behaviour is highly deceiving because certain Stream implementations can optimize out peek() calls. Let’s use the infamous mutable : Stream.of(om(Instant.EPOCH))Īnd we can observe that the result is far away from the standard epoch, which means the mutating operation was indeed applied. So, technically, we should be able to, e.g., modify the Stream elements on the fly which would not be possible in the immutable, functional world. We can see that non-debugging usages are neither forbidden nor discouraged. Let’s have a peek(pun intended) into the English dictionary: The point of confusion is the “mainly” word. This method exists mainly to support debugging, where you want to see the elements as they flow past a certain point in a pipeline Proper Usageįurther inspection of the official docs reveals a note: Unfortunately, Streams do not always behave entirely lazily. So, we can see that peek() can’t be treated as an intermediate for-each replacement because it invokes the passed Consumer only on elements that are visited by the Stream. Of course, if you try to collect the whole infinite sequence to some data structure, even laziness will not save you. The above operation completes almost immediately because of the lazy character of Stream traversal. (Because where do you store an infinite sequence? In the cloud?) erate(0, i -> i + 1) This is why it’s possible to represent and manipulate infinite sequences using Streams. Additionally, the traversal is triggered only when a terminal method is present. ![]() So, because of the lazy evaluation Stream pipelines are always being traversed “vertically” and not “horizontally” which allows avoiding doing unnecessary calculations. Traversal of the pipeline source does not begin until the terminal operation of the pipeline is executed. ![]() They are always lazy executing an intermediate operation such as filter() does not actually perform any filtering, but instead creates a new stream that, when traversed, contains the elements of the initial stream that match the given predicate. Intermediate operations return a new stream. peek(e -> (e + " was consumed by peek().")) It gets even more interesting if we get rid of the forEach() call: Stream.of("one", "two", "three") It might be tempting to think that we’ll see a series of peek() logs followed by a series of for-each logs but this is not the case: one was consumed by peek(). Stream/peek Lazy Evaluationīut… what will happen if we replace the terminal collect() operation with a forEach()? Stream.of("one", "two", "three") The result of such operation is not surprising: one was consumed by peek(). We can use it for applying side-effects for every consumed Stream element: Stream.of("one", "two", "three") 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. Let’s start responsibly by RTFM inspecting peek()’s user’s manual. Let’s try to clarify how it works and when should be used. Want to learn more? Read the breakdown of the difference between peeked, peaked, and piqued.The peek() method from the Java Stream API is often misunderstood. Here’s an example of peek, peak, and pique used correctly in a sentence.Įxample: The image of the strange mountain peak on the cover was meant to pique your curiosity, and it worked-you picked up the magazine to peek inside. To remember what pique means (and how to spell it), remember that the things that pique your interest or curiosity are often unique. Another way to remember this is that peek has two e’s like look has two o’s. Since peek and peak are so close in spelling, one way to tell them apart is to remember that peek has two e’s, which kind of look like two little eyes peeking out between the p and the k. The confusion surrounding the three words-all three of which can be used as both a verb and a noun-is mainly due to the fact that they are all pronounced the same. The less frequently used word pique is most commonly used as a verb meaning to arouse or excite a particular feeling, as in Advertisements are designed to pique your interest. The word peak refers to the top of a mountain or, more figuratively, to the highest point of something, as in the peak of my career. The word peek is most commonly used as a verb meaning to look at something quickly or sneakily and as a noun referring to such a look. ![]()
0 Comments
Leave a Reply. |