Skip to content

Commit 5b04fe7

Browse files
committed
feat(streams): add demo of Primitive Streams (IntStream, DoubleStream) with ranges and random numbers
What - Implemented `PrimitiveStreams` class showcasing usage of specialized primitive streams. - Demonstrated: - `IntStream.range(1, 5)` → half-open range [1, 5). - `IntStream.rangeClosed(1, 5)` → inclusive range [1, 5]. - `IntStream.of(1, 2, 3)` → creating primitive stream directly. - `DoubleStream` from `Random.doubles(5)` → generates 5 random doubles. - `IntStream` from `Random.ints(5)` → generates 5 random ints. - Converted primitive streams to boxed object streams via `.boxed().collect(Collectors.toList())` for printing. Why - Primitive streams (`IntStream`, `DoubleStream`) avoid autoboxing overhead of `Stream<Integer>` or `Stream<Double>`. - Provide specialized APIs (`sum()`, `average()`, `min()`, `max()`, `summaryStatistics()`). - Useful in numerical-heavy domains like analytics, simulations, financial systems. - Demonstrates difference between range vs rangeClosed and practical generation of random values. Logic 1. **Creating Streams** - `Arrays.stream(numbers)` → from int[]. - `IntStream.range(1, 5)` → produces `[1,2,3,4]`. - `IntStream.rangeClosed(1, 5)` → produces `[1,2,3,4,5]`. - `IntStream.of(1,2,3)` → direct literal stream. 2. **Random Primitive Streams** - `new Random().doubles(5)` → generates 5 random doubles. - `new Random().ints(5)` → generates 5 random integers. - Streams are infinite by default; specifying limit avoids unbounded generation. 3. **Boxing for Display** - `.boxed()` converts primitive values into wrapper objects (`Integer`, `Double`). - Collected into `List` for easy printing. 4. **Statistical APIs (commented for clarity)** - `.sum()`, `.min()`, `.max()`, `.average()`, `.summaryStatistics()`. - These methods directly support numerical operations without manual reduction. Notes - Use `.boxed()` only when interoperability with object-based APIs is required. - Prefer primitive stream methods (`sum`, `average`) for performance. - `range()` vs `rangeClosed()` → subtle but critical for loop-like semantics. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 365d1ee commit 5b04fe7

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

JAVA8/StreamsInJAVA/PrimitiveStreams/src/PrimitiveStreams.java renamed to Java 8 Crash Course/Java 8 Streams/Primitive Streams/src/PrimitiveStreams.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ public static void main(String[] args) {
1515
IntStream.of(1, 2, 3);
1616

1717
DoubleStream doubles = new Random().doubles(5);
18-
// System.out.println(doubles.sum());
19-
// System.out.println(doubles.min());
20-
// System.out.println(doubles.max());
21-
// System.out.println(doubles.average());
22-
// doubles.summaryStatistics()
23-
// doubles.mapToInt(x -> (int) (x + 1));
18+
// System.out.println(doubles.sum());
19+
// System.out.println(doubles.min());
20+
// System.out.println(doubles.max());
21+
// System.out.println(doubles.average());
22+
// doubles.summaryStatistics()
23+
// doubles.mapToInt(x -> (int) (x + 1));
2424
System.out.println(doubles.boxed().toList());
2525

2626
IntStream intStream = new Random().ints(5);
2727
System.out.println(intStream.boxed().toList());
2828
}
29-
}
29+
}

0 commit comments

Comments
 (0)