Commit b249879
committed
feat(paradigms): add imperative vs declarative programming examples with Java Streams
What
- Added two demo classes (`Main` and `Test`) to illustrate the contrast between **imperative** and **declarative** styles in Java.
- Both programs solve the same problem: **sum of even numbers** from a collection.
- Demonstrations include:
- **Imperative style**: explicit loops, conditions, and accumulation (`for` loop + if-check + manual sum).
- **Declarative style**: Java Streams API with `filter`, `mapToInt`, and `sum` operations.
- Included explanatory comments in code highlighting:
- Focus shift: *HOW (imperative)* vs *WHAT (declarative)*.
- Advantages of declarative: reduced boilerplate, better readability, parallelization support.
Why
- Developers often default to imperative loops; this commit clarifies how Streams offer a **functional declarative alternative**.
- Establishes a clear, side-by-side comparison to help learners understand the shift in mindset.
- Highlights **practical benefits**: shorter code, cleaner intent, and easier maintainability.
Logic
1. Imperative Example
- Initialize accumulator (`sum = 0`).
- Loop through each element in list/array.
- Manually check if even (`n % 2 == 0`).
- Update accumulator.
- Print final sum.
- Explicitly controls every step → verbose but predictable.
2. Declarative Example
- Express *what* is needed:
- Convert collection to a stream.
- Apply a filter predicate (`n % 2 == 0`).
- Convert to primitive stream (`mapToInt` for Integer).
- Call `sum()` terminal operation.
- Lets the Streams API handle iteration internally.
- Concise, more readable, and easily parallelizable (`.parallelStream()`).
Real-life applications
- **Data analytics**: summing/filtering business metrics (sales, transactions).
- **ETL pipelines**: transforming and aggregating raw datasets declaratively.
- **Backend APIs**: summarizing DB query results without verbose loops.
- **Parallel processing**: declarative pipelines can scale across CPU cores automatically.
Notes
- Imperative code gives fine-grained control but leads to boilerplate.
- Declarative code is expressive but delegates execution details to the runtime.
- Both paradigms coexist; choice depends on clarity, performance needs, and context.
- Declarative style in Java heavily relies on Streams, Lambdas, and Collectors introduced in Java 8.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 1c52d58 commit b249879
File tree
1 file changed
+54
-0
lines changed- Java 8 Crash Course/Java 8 Streams/Declarative Programming/src
1 file changed
+54
-0
lines changedLines changed: 54 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
0 commit comments