Leveraging the Laziness of Streams

In previous chapters we’ve seen the facilities that the Stream interface offers, but so far we haven’t discussed one of their most salient features—they’re really lazy, in a good way. The lazy evaluation of Streams is powerful. First, we don’t have to do anything special to derive their benefits. In fact, we’ve used them many times already! Second, they can postpone not just one, but a sequence of evaluations so that only the most essential parts of the logic are evaluated, and only when needed. Let’s look at how lazy Streams are and how we benefit from that.

Intermediate and Terminal Operations

Streams have two types of methods: intermediate and terminal, which work together. The secret behind their laziness ...

Get Functional Programming in Java, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.