Skip to content

Commit 108e4b3

Browse files
committed
feat(streams): demonstrate multiple ways to create streams in Java
What - Added `Test` class in `Stream` package to showcase **stream creation techniques**: 1. From a **Collection** (`list.stream()`). 2. From an **Array** (`Arrays.stream(array)`). 3. Using **Stream.of(...)** with direct values. 4. Using **Stream.iterate(seed, unaryOperator)`** to generate a sequence (incrementing numbers). 5. Using **Stream.generate(Supplier)`** to create infinite streams with custom logic, later truncated using `.limit(...)`. - Example cases: - `list.stream()` → creates a stream from a list of fruits. - `Arrays.stream(array)` → creates a stream from an array of fruits. - `Stream.of(1, 2, 3)` → simple stream of integers. - `Stream.iterate(0, n -> n + 1).limit(100)` → generates first 100 natural numbers. - `Stream.generate(() -> Integer.valueOf("Hello")).limit(5)` → ⚠️ will throw `NumberFormatException` because `"Hello"` is not numeric. - `Stream.generate(() -> (int) Math.random() * 100).limit(100)` → intended to generate 100 random integers, but due to operator precedence bug, always produces `0`. Why - To illustrate **stream creation flexibility** in Java: - Collections → most common entry point. - Arrays → convert legacy structures into streams. - Factory methods (`of`, `iterate`, `generate`) → allow programmatic sequence generation. - Reinforces understanding of **finite vs infinite streams** and the necessity of `.limit()` with infinite sources. - Demonstrates pitfalls (incorrect `generate` usage and operator precedence issue). Logic 1. **Collection-based streams** - `list.stream()` returns a sequential stream tied to the list. 2. **Array-based streams** - `Arrays.stream(array)` provides stream over array elements. 3. **Direct factory creation** - `Stream.of(...)` is a shortcut for small, fixed sets. 4. **Iterative infinite streams** - `Stream.iterate(seed, func)` applies function repeatedly starting from seed. - Example: seed=0, func=n+1 → generates 0,1,2,... - `.limit(100)` truncates it to 100 elements. 5. **Supplier-based infinite streams** - `Stream.generate(Supplier)` repeatedly calls the supplier. - Example: `Math.random()` for pseudo-random doubles. - Must use `.limit(n)` to avoid unbounded growth. 6. **Bugs to highlight** - `Integer.valueOf("Hello")` → runtime error. - `(int) Math.random() * 100` → due to precedence, cast happens before multiplication → always `0`. Correct version: `(int) (Math.random() * 100)`. Key Takeaways - Streams can be created from collections, arrays, literals, or generators. - Infinite streams (`iterate`, `generate`) **must be bounded** with `.limit()`. - Be cautious with operator precedence and data conversion to avoid runtime bugs. - Stream creation is the foundation for later intermediate (map/filter) and **terminal (collect, reduce) operations. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 2d5139e commit 108e4b3

File tree

1 file changed

+24
-0
lines changed
  • Java 8 Crash Course/Java 8 Streams/src/Stream

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package Stream;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
import java.util.stream.Stream;
6+
7+
public class Test {
8+
public static void main(String[] args) {
9+
List<String> list = Arrays.asList("Apple","Banana","Cherry");
10+
Stream<String> myStream = list.stream();
11+
12+
String[] array = {"Apple","Banana","Cherry"};
13+
14+
Stream<String> stream = Arrays.stream(array);
15+
16+
Stream<Integer> integerStream = Stream.of(1,2,3);
17+
Stream<Integer> limit = Stream.iterate(0,n -> n + 1).limit(100);
18+
System.out.println(limit);
19+
20+
Stream<Integer> limit1 = Stream.generate(() -> Integer.valueOf("Hello")).limit(5);
21+
22+
Stream<Integer> limit2 = Stream.generate(() -> (int) Math.random() * 100).limit(100);
23+
}
24+
}

0 commit comments

Comments
 (0)