Skip to content

Commit 2d5139e

Browse files
committed
feat(streams): add factorial computation and filter-peek-count demo using Java Streams
What - Added `FactorialDemo`: - Demonstrates computing factorial using Java 8 Streams (`IntStream.rangeClosed` + `reduce`). - `printFactorial(int n)` → calculates factorial by reducing the range [1..n] with multiplication. - `solve(int n)` wrapper method introduced for readability. - Prints factorial of `6` → output `720`. - Added `StreamDemo`: - Demonstrates filtering, peeking, and counting with streams. - Input list: ["a", "b", "c", "d", "e"]. - `.filter(s -> s.compareTo("c") > 0)` → keeps only elements after "c" alphabetically (`"d"`, `"e"`). - `.peek(System.out::println)` → prints filtered elements (side effect for debugging). - `.count()` → terminal operation that counts elements post-filter (expected = `2`). - Prints elements `"d"`, `"e"`, then the count `2`. Why - To illustrate **two key stream use-cases**: 1. **Mathematical computation (factorial)** using `reduce` and `IntStream`. 2. **Data filtering + debugging + aggregation** with filter, peek, and count. - Reinforces understanding of lazy evaluation (peek prints only when terminal op is triggered). - Shows declarative programming style vs imperative loops. Logic 1. **FactorialDemo** - Stream pipeline: `IntStream.rangeClosed(1, n)` → sequence [1..n]. - Reduce operation: `(a, b) -> a * b` multiplies accumulatively. - Initial identity = `1` ensures neutral start for multiplication. - For n=6 → factorial = `1*2*3*4*5*6 = 720`. 2. **StreamDemo** - List of strings converted to stream. - Filter keeps strings alphabetically greater than `"c"`. - Peek prints elements after filter (`"d"`, `"e"`). - Count triggers evaluation, resulting in `2`. Key Takeaways - Streams allow **functional-style computation** (factorial via reduce). - Intermediate operations (`filter`, `peek`) are lazy → only run when terminal op (`count`) is called. - `peek()` is invaluable for debugging pipelines without breaking them. - Terminal operations consume the stream; reuse is not allowed. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent da05615 commit 2d5139e

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed
Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
package Stream;
2+
13
import java.util.Arrays;
24
import java.util.List;
35

4-
public class StreamExample {
6+
public class StreamDemo {
57
public static void main(String[] args) {
68
List<String> list = Arrays.asList("a", "b", "c", "d", "e");
79

@@ -17,17 +19,23 @@ public static void main(String[] args) {
1719
/*
1820
This question tests your understanding of Java Streams, specifically filter, peek, and count.
1921
20-
The problem asks for the output of a given Java code snippet. Let's break down the code step by step:
22+
The problem asks for the output of a given Java code snippet.
2123
2224
List<String> list = Arrays.asList("a", "b", "c", "d", "e"); - This creates a List of strings.
2325
.stream() - This converts the list into a stream.
24-
.filter(s -> s.compareTo("c") > 0) - This filters the stream, keeping only elements where the comparison with "c" is greater than 0. s.compareTo("c") > 0 means that string s comes after "c" alphabetically. So, "d" and "e" will pass this filter.
25-
.peek(System.out::println) - This is an intermediate operation that allows you to perform an action on each element as it passes through the stream. In this case, it will print each element that has passed the filter.
26-
.count() - This is a terminal operation that counts the number of elements in the stream after all the intermediate operations have been applied.
26+
.filter(s -> s.compareTo("c") > 0) - This filters the stream, keeping only elements where the
27+
comparison with "c" is greater than 0.
28+
s.compareTo("c") > 0 means that string s comes after "c" alphabetically. So, "d" and "e" will pass this filter.
29+
.peek(System.out::println) - This is an intermediate operation that allows you to perform an action on each element
30+
as it passes through the stream.
31+
32+
In this case, it will print each element that has passed the filter.
33+
.count() - This is a terminal operation that counts the number of elements in the stream after all the
34+
intermediate operations have been applied.
2735
2836
Based on this analysis, the elements "d" and "e" will pass the filter. The peek operation will then print "d" and then "e".
2937
30-
Finally, the count operation will return the number of elements that passed the filter, which is 2. The last System.out.println(count); will print this count.
38+
Finally, the count operation will return the number of elements that passed the filter, which is 2.
3139
32-
Therefore, the output will be "d", followed by "e", followed by "2".
33-
*/
40+
The last System.out.println(count); will print this count.
41+
*/

0 commit comments

Comments
 (0)