Skip to content

Commit b249879

Browse files
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 changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import java.util.Arrays;
2+
import java.util.List;
3+
4+
public class Main {
5+
public static void main(String[] args) {
6+
List<Integer> numbers = Arrays.asList(1,2,3,4,5,6,7,8,9,10);
7+
8+
// Imperative Programming
9+
// Focus: HOW to do it step-by-step
10+
int sumImperative = 0;
11+
for (int n : numbers) {
12+
if (n % 2 == 0) {
13+
sumImperative += n;
14+
}
15+
}
16+
System.out.println("Imperative Sum of Even Numbers: " + sumImperative);
17+
18+
// Declarative Programming
19+
// Focus: WHAT result we want
20+
int sumDeclarative = numbers.stream() // convert list to stream
21+
.filter(n -> n % 2 == 0) // keep only even numbers
22+
.mapToInt(Integer::intValue) // convert Integer → int
23+
.sum();
24+
System.out.println("Declarative Sum of Even Numbers: " + sumDeclarative);
25+
}
26+
}
27+
28+
/*
29+
1. Imperative Style:
30+
- You tell the computer **how** to achieve the result.
31+
- Example here:
32+
➝ create a variable sum = 0
33+
➝ loop through list
34+
➝ check even condition manually
35+
➝ add to sum
36+
- Code is longer, more error-prone, but gives explicit control.
37+
38+
2. Declarative Style:
39+
- You tell the computer **what** you want.
40+
- Example here:
41+
➝ take numbers
42+
➝ filter evens
43+
➝ sum them up
44+
- No explicit loop or condition handling written by you.
45+
- Java Streams API handles the details internally.
46+
47+
3. Key Difference:
48+
- Imperative = *HOW to do it (step-by-step instructions)*.
49+
- Declarative = *WHAT result you want (describe the logic)*.
50+
51+
✔ Imperative: Loop + condition + manual sum.
52+
✔ Declarative: Streams + filter + sum → concise, readable.
53+
✔ Declarative programming improves readability and reduces boilerplate.
54+
*/

0 commit comments

Comments
 (0)