Skip to content

Commit f346efd

Browse files
committed
Add FiboNumbers class to generate and print the first 10 Fibonacci numbers using Java 8 Stream API
1 parent 86a5a5f commit f346efd

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.java8;
2+
3+
// Provides Stream API utilities used to generate Fibonacci pairs.
4+
import java.util.stream.Stream;
5+
6+
// Demonstrates generating Fibonacci numbers using Java 8 Stream.iterate.
7+
public class FiboNumbers {
8+
// Entry point of the program.
9+
public static void main(String[] args) {
10+
// Start with the first Fibonacci pair: (0, 1).
11+
Stream.iterate(new int[]{0, 1}, x -> new int[]{x[1], x[0] + x[1]})
12+
// Generate only the first 10 Fibonacci values.
13+
.limit(10)
14+
// From each pair, take the first value as the current Fibonacci number.
15+
.map(x -> x[0])
16+
// Print each Fibonacci number.
17+
.forEach(System.out::println);
18+
}
19+
}

0 commit comments

Comments
 (0)