The JDK8 new features is the Stream. It is a little confusing at first but let us explain this to you in a moment.

//Generating fibonacci sequence using Java8 Stream 
Stream.iterate(new int[] {0,1}, t -> new int[] {t[1], t[0] + t[1]}) 
.limit(10) 
.forEach(t -> System.out.println(t[0]));

By using Java8’s Stream class, we are able to create a code that prints out the Fibonacci sequence. As simple as that!

What? Wait a minute. I don’t understand a thing there.

Breakdown

The best way to learn java stream is to run the code in Java and start experimenting.

Stream.iterate(START, NEXT_ACTION) – you can look at the first line as like this. In the START, we are initializing an integer array with values 0 and 1. The NEXT_ACTION, we are saying that we are initializing again a new integer array with the first value from the previous step and the sum of both values for the second value.

Iteration #1 – 0, 1
Iteration #2 – 1, 1
Iteration #2 – 1, 2
Iteration #4 – 2, 3
Iteration #5 – 3, 5
Iteration #6 – 5, 8

We limit the looping to just 10. Easy!

.limit(10)

The forEach is for looping thru each items again in the stream and saying that each t[0] goes to be printed on screen.

.forEach(t -> System.out.println(t[0])); 

The “t ->” can be any letter or word. It just signifies a variable that can be used after the “->”.