From Wikipedia, The **Fibonacci numbers** are the numbers in the following integer sequence, called the **Fibonacci sequence**, and characterized by the fact that every number after the first two is the sum of the two preceding ones:

Often, especially in modern usage, the sequence is extended by one more initial term:

By definition, the first two numbers in the Fibonacci sequence are either 1 and 1, or 0 and 1, depending on the chosen starting point of the sequence, and each subsequent number is the sum of the previous two.

The sequence *F _{n}* of Fibonacci numbers is defined by the recurrence relation:

- with seed values
or

In this article I am going to show, how to generate fibonacci sequence using Java Stream API.

So here is the code:

public class Fibonacci { public List<Integer> generate(int series) { return Stream.iterate(new int[]{0, 1}, i -> new int[]{i[1], i[0] + i[1]}) .limit(series) .map(i -> i[0]) .collect(Collectors.toList()); } }

Let’s run it:

public class FibonacciDemo { public static void main(String[] args) { Fibonacci fibonacci = new Fibonacci(); fibonacci.generate(10); } }

The output will be:

[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

Now, Let me explain what is happening inside the code.

`Stream.iterate()`

Java Stream API provides two static methods in the Stream interface for creating infinite streams. These are `Stream.iterate()`

and `Stream.generate()`

. `Stream.iterate(T seed, UnaryOperator<T> f)`

returns an infinite sequential ordered `Stream`

produced by iterative application of a function `f`

to an initial element `seed`

, producing a `Stream`

consisting of `seed`

, `f(seed)`

, `f(f(seed))`

, etc.

`Stream.iterate()`

method works just like a ** function-of** algebraic operation which is commonly written as

**ƒ(x)**. The method first returns the seed-value itself. For the 2

^{nd}element in the Stream it finds

**ƒ(seed-value)**and from then on iteratively keeps applying

**to the returned values.**

*function-of*In our case, the seed value is [0, 1] and the function is `i -> new int[]{i[1], i[0] + i[1]}`

So the first value returned from the infinite stream will be [0, 1],

the second value returned from the infinite stream will be [1, 1],

the third value returned from the infinite stream will be [1, 2],

the fourth value returned from the infinite stream will be [2, 3],

the fifth value returned from the infinite stream will be [3, 5] and so on…

`Stream.limit()`

Since infinite streams need to be limited to a finite number, based on specific requirement, hence it is a common practice to limit the number of elements produced by a stream using the `Stream.limit()`

method. `Stream.limit(long maxSize)`

returns a stream consisting of the elements of this stream, truncated to be no longer than `maxSize`

in length. In our example, I have limit the numbers of elements produced by the stream to the number of series required.

`Stream.`

map()

`Stream.`

map()

returns a stream consisting of the results of applying the given function to the elements of this stream. In our example, I have mapped it to a function to return only the first element of the integer pairs.`Stream.`

map(Function<? super T,? extends R> mapper)

`Stream.collect()`

`Stream.collect()`

Performs a mutable reduction operation on the elements of the stream using a `Stream.`

collect()`Collector`

.

`Collectors.toList()`

`Collectors`

is an implementations of `Collector`

that implement various useful reduction operations, such as accumulating elements into collections, summarizing elements according to various criteria, etc. `Collectors.toList()`

returns a `Collector`

that accumulates the input elements into a new `List`

. In our example, we are returning the output sequence in a list.

### Summary

That’s it for now. We have learnt what is a Fibonacci sequence and then implemented it using Java Stream API and discussed about the implementation. Thanks for reading.