Skip to content

Commit d6c607a

Browse files
committed
feat(FunctionalProgramming): add Main demonstrating streams with filter and sum
What - Added Main class in package FunctionalProgramming. - Demonstrates Java Streams API with filter, mapToInt, and sum. - Key operations: - Created numbers list with integers 1 to 10. - First filter (x -> x > 100000) without terminal operation — no effect (lazy evaluation). - Second pipeline: filter even numbers, convert to IntStream, compute sum. - Printed result (30). Why - Example shows functional programming with streams instead of loops. - Demonstrates how filters and aggregations are composed in pipelines. - Highlights lazy evaluation: without terminal operation, intermediate steps don’t execute. - Provides educational example of filtering and aggregating with concise code. How - Declared numbers = Arrays.asList(1…10). - Called numbers.stream().filter(x -> x > 100000); (unused, no terminal op). - Created second stream: - filter(n -> n % 2 == 0) → selects even numbers. - mapToInt(n -> n) → converts to IntStream. - sum() → aggregates total. - Printed sum. Logic - Inputs: list [1,2,3,4,5,6,7,8,9,10]. - Outputs: integer 30 printed to stdout. - Flow: 1. Build list of integers. 2. First stream + filter defined but no terminal op → no output, no execution. 3. Second stream pipeline executes: - Filter → [2,4,6,8,10]. - MapToInt → [2,4,6,8,10] (IntStream). - sum() → 30. 4. Print 30. - Edge cases: - If list is empty, sum() returns 0 safely. - First filter is dead code since not consumed. - Complexity / performance: O(n) for stream traversal; efficient for small lists. - Concurrency / thread-safety: - List is immutable (from Arrays.asList). - Stream operations are stateless and safe here. - Error handling: no exceptions expected. Real-life applications - Calculating aggregates (sum, average) with concise stream pipelines. - Filtering datasets with conditions (even numbers, IDs > threshold). - Functional programming style avoids explicit loops and mutable accumulators. - Basis for scalable data processing (stream pipelines can be parallelized). Notes - To make first filter effective, add terminal operation like forEach or collect. - mapToInt(n -> n) can be simplified to mapToInt(Integer::intValue). - Example shows laziness of streams — key concept in functional programming. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 32a0999 commit d6c607a

File tree

1 file changed

+42
-0
lines changed
  • Java 8 Crash Course/Predicate/src/FunctionalProgramming

1 file changed

+42
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package FunctionalProgramming;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
import java.util.function.Predicate;
7+
8+
public class Main {
9+
public static void main(String[] args) {
10+
List<Integer> numbers = Arrays.asList(1,2,3,4,5,6,7,8,9,10);
11+
numbers.stream().filter(x -> x > 100000);
12+
13+
int sum = numbers.stream().filter(n -> n % 2 == 0).mapToInt(n -> n).sum();
14+
System.out.println(sum);
15+
}
16+
}
17+
18+
/*
19+
1. numbers list:
20+
- `Arrays.asList(1,2,3,4,5,6,7,8,9,10)` → ek fixed list of integers banayi.
21+
22+
2. Stream + Filter (x -> x > 100000):
23+
- `numbers.stream().filter(x -> x > 100000);`
24+
- Ye line technically stream banata hai aur filter karta hai,
25+
but koi terminal operation (like `collect`, `forEach`) nahi diya gaya →
26+
isliye ye actually kuch **execute nahi karega**.
27+
- Matlab "lazy evaluation" hogi aur ye line ka koi output nahi aayega.
28+
29+
3. Even numbers filter + Sum:
30+
- `numbers.stream().filter(n -> n % 2 == 0)`
31+
→ Sirf even numbers (2,4,6,8,10) pass honge.
32+
- `.mapToInt(n -> n)`
33+
→ Unko int stream me convert kiya for calculation.
34+
- `.sum()`
35+
→ Sab even numbers add kiye → 2+4+6+8+10 = 30.
36+
37+
✔ Streams = sequence of elements processed with pipeline operations.
38+
✔ Filter = condition apply karta hai (true wale elements aage pass hote hain).
39+
✔ MapToInt + sum = transformation + aggregation.
40+
✔ Is program me → even numbers ka sum nikalta hai (30).
41+
✔ Pehla filter line kaam nahi karti because koi terminal operation nahi hai.
42+
*/

0 commit comments

Comments
 (0)