Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Idiomatic Peeking With the Java Stream API

DZone's Guide to

Idiomatic Peeking With the Java Stream API

Often used in debugging, the peek() method in the Java Stream API has some caveats to consider due to internal optimization before implementing it.

· Java Zone
Free Resource

The single app analytics solutions to take your web and mobile apps to the next level.  Try today!  Brought to you in partnership with CA Technologies

The peek() method from the Java Stream API is often misunderstood. Let's try to clarify how it works and when should be used.

Stream<T> peek(Consumer<? super T> action)

Introduction

Let's start responsibly by RTFM inspecting peek()'s user's manual.

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.

Sounds pretty straightforward. We can use it for applying side-effects for every consumed Stream element:

Stream.of("one", "two", "three")
  .peek(e -> System.out.println(e + " was consumed by peek()."))
  .collect(Collectors.toList());


The result of such operation is not surprising:

one was consumed by peek().
two was consumed by peek().
three was consumed by peek().


Stream/Peek Lazy Evaluation

But... what will happen if we replace the terminal collect() operation with a forEach()?

Stream.of("one", "two", "three")
  .peek(e -> System.out.println(e + " was consumed by peek()."))
  .forEach(System.out::println);


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().
one
two was consumed by peek().
two
three was consumed by peek().
three


It gets even more interesting if we get rid of the forEach call:

Stream.of("one", "two", "three")
  .peek(e -> System.out.println(e + " was consumed by peek()."));


The result would be nothing:


As the Javadoc states:

Intermediate operations return a new stream. 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. Traversal of the pipeline source does not begin until the terminal operation of the pipeline is executed.

So, because of the lazy evaluation, Stream pipelines are always being traversed "vertically" and not "horizontally," which allows avoiding doing unnecessary calculations. Additionally, the traversal is triggered only when a terminal method is present. Hence, the observed behavior.

This is why it's possible to represent and manipulate infinite sequences using Streams. (Because where do you store an infinite sequence? In the cloud?):

Stream.iterate(0, i -> i + 1)
  .peek(System.out::println)
  .findFirst();


The above operation completes almost immediately because of the lazy character of Stream traversal. Of course, if you try to collect the whole infinite sequence to some data structure, even laziness will not save you.

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.

Unfortunately, Streams do not always behave entirely lazily.

Proper Usage

Further inspection of the official docs reveals a note:

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

The point of confusion is the "mainly" word. Let's have a peek (pun intended) at the English dictionary:

We can see that non-debugging usages are not forbidden nor discouraged.

So, technically, we should be able to, for example, modify the Stream elements on the fly which would not be possible in the immutable, functional world. Let's use the infamous mutable java.util.Date:

Stream.of(Date.from(Instant.EPOCH))
  .peek(d -> d.setTime(Long.MAX_VALUE))
  .forEach(System.out::println);

// Sun Aug 17 08:12:55 CET 292278994


And we can observe that the result is far away from the standard epoch, which means the mutating operation was indeed applied.

The problem is that this behavior is highly deceiving because certain Stream implementations can optimize out peek() calls.

This gets clarified in the early draft of JDK 9's docs eventually:

In cases where the stream implementation is able to optimize away the production of some or all the elements (such as with short-circuiting operations like findFirst, or in the example described in count()), the action will not be invoked for those elements. (...)

An implementation may choose to not execute the stream pipeline (either sequentially or in parallel) if it is capable of computing the count directly from the stream source.

So, now it's clear that it might not be the best choice if we want to perform some side effects deterministically.

According to this, peek() might not be even that reliable debugging tool after all.

Key Takeaways

  • The peek() method works fine as a debugging tool when we want to see what is being consumed by a Stream.
  • It seems to work fine when applying mutating operations but should not be used this way because this behavior is non-deterministic due to the possibility of certain peek() calls being omitted due to internal optimization.
  • The discussion of whether mutation operations should be allowed would never take place if we were restricted to operate only on immutable values.
  • We have around 292,276,977 years before we run out of java.util.Date range.

CA App Experience Analytics, a whole new level of visibility. Learn more. Brought to you in partnership with CA Technologies.

Topics:
java ,stream api ,peek method ,lazy evaluation

Published at DZone with permission of Grzegorz Piwowarek. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}