Commit 1d2acb6
committed
feat(JAVA8): add demo combining lambdas, streams, and default interface methods
What
- Added JAVA8 class implementing Greetable interface.
- Greetable defines default method greet(String name) → prints greeting.
- Demonstrates multiple Java 8 features:
- Lambda expression with forEach to print list of names.
- Stream API:
- Filtering even numbers and summing them.
- Filtering names starting with "C" and collecting results.
- Default method in interface called through JAVA8 instance.
Why
- Provides consolidated example of Java 8 functional programming and interface enhancements.
- Shows how lambdas reduce boilerplate for iteration.
- Highlights stream transformations and aggregations with filter, mapToInt, sum, and collect.
- Demonstrates backward-compatible interface evolution with default methods.
How
- Created list of names ["Alice","Bob","Charlie"].
- Used forEach with lambda to print each name.
- Created list of numbers [1–6].
- Built stream pipeline:
- filter even numbers → [2,4,6].
- mapToInt → [2,4,6].
- sum → 12.
- Built another pipeline:
- filter names starting with "C" → ["Charlie"].
- collect into new list.
- Instantiated JAVA8 class and invoked greet("Developer").
Logic
- Inputs:
- List<String> names = ["Alice","Bob","Charlie"].
- List<Integer> numbers = [1,2,3,4,5,6].
- Outputs:
- Printed names line by line.
- Printed "Sum of even numbers: 12".
- Printed "Names starting with 'C': [Charlie]".
- Printed "Hello, Developer!".
- Flow:
1. Lambda prints list elements.
2. Stream sums even integers.
3. Stream filters names by prefix.
4. greet() prints default method message.
- Edge cases:
- Empty lists → no output from streams.
- greet() works for any non-null string.
- Complexity / performance: O(n) per stream pipeline.
- Concurrency / thread-safety: Lists are immutable (Arrays.asList). Stream operations stateless and safe here.
- Error handling: None required.
Real-life applications
- Printing collections with lambdas instead of loops.
- Summing filtered values (e.g., transactions, scores).
- Filtering data by conditions and collecting into lists.
- Using default methods to add behavior to interfaces without breaking implementations.
Notes
- Method references (System.out::println) could simplify lambda in forEach.
- Streams are lazy; execution triggered by terminal ops like sum() or collect().
- Default methods help evolve interfaces with minimal disruption.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 7c28c51 commit 1d2acb6
1 file changed
+39
-0
lines changed| 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 | + | |
0 commit comments