17.15 Infinite Streams
A data structure, such as an array or a collection, always represents a finite number of elements—all the elements are stored in memory, and memory is finite. Of course, any stream created from a finite data structure will have a finite number of elements, as has been the case in this chapter’s prior examples.
Lazy evaluation makes it possible to work with infinite streams that represent an unknown, potentially infinite, number of elements. For example, you could define a method nextPrime
that produces the next prime number in sequence every time you call it. You could then use this to define an infinite stream that conceptually represents all prime numbers. However, because streams are lazy until you perform a terminal ...
Get Java How to Program, Early Objects, 11th 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.