The following examples make extensive use of the new features in Java 8 to implement the concept of a stream: a classical data structure similar to a list that uses lazy evaluation. This is not exactly how they have defined the new streams API in Java 8 (you can find other articles I have written about this at the end), but for me it worked as an excellent exercise to understand important concepts like lazy evaluation, the creation of closures through lambda expressions and the value of the new features like default methods and static methods in interfaces. The concept is based on the same idea of a stream developed in Lisp by Abelson and Gerald and Julie Sussman in their book Structure and Interpretation of Computer Programs. In this article I will develop several examples of infinite streams with Java, particularly an infinite stream of natural numbers, and on top of that, an infinite stream of prime numbers using the sieve of Eratosthenes and finally an infinite stream of Fibonacci numbers.
Apr 03, 13