Java Reference
In-Depth Information
The iterate method takes an initial value, here 0, and a lambda (of type Unary-Operator<T>) to
apply successively on each new value produced. Here you return the previous element added
with 2 using the lambda n -> n + 2. As a result, the iterate method produces a stream of all even
numbers: the first element of the stream is the initial value 0. Then it adds 2 to produce the new
value 2; it adds 2 again to produce the new value 4 and so on. This iterate operation is
fundamentally sequential because the result depends on the previous application. Note that this
operation produces an infinite stream —the stream doesn't have an end because values are
computed on demand and can be computed forever. We say the stream is unbounded . As we
discussed earlier, this is a key difference between a stream and a collection. You're using the
limit method to explicitly limit the size of the stream. Here you select only the first 10 even
numbers. You then call the forEach terminal operation to consume the stream and print each
element individually.
In general, you should use iterate when you need to produce a sequence of successive values, for
example, a date followed by its next date: January 31, February 1, and so on. To see a more
difficult example of how you can apply iterate, try out Quiz 5.4 .
Quiz 5.4: Fibonacci tuples series
The Fibonacci series is famous as a classic programming exercise. The numbers in the following
sequence are part of the Fibonacci series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55.... The first two numbers
of the series are 0 and 1, and each subsequent number is the sum of the previous two.
The series of Fibonacci tuples is similar; you have a sequence of a number and its successor in
the series: (0, 1), (1, 1), (1, 2), (2, 3), (3, 5), (5, 8), (8, 13), (13, 21)....
Your task is to generate the first 20 elements of the series of Fibonacci tuples using the iterate
method!
Let us help you get started. The first problem is that the iterate method takes a
UnaryOperator<T> as argument and you need a stream of tuples such as (0, 1). You can, again
rather sloppily, use an array of two elements to represent a tuple. For example, new int[]{0, 1}
represents the first element of the Fibonacci series (0, 1). This will be the initial value of the
iterate method:
Stream.iterate(new int[]{0, 1}, ???)
.limit(20)
.forEach(t -> System.out.println("(" + t[0] + "," + t[1] +")"));
Search WWH ::




Custom Search