Commit ecc52a4
committed
feat(Package1): add Test class showcasing Optional map transformations
What
- Added Test class in package Package1.
- Demonstrates functional-style operations on Optional using map and ifPresent.
- main method:
- Calls getName(2) returning Optional<String> with "ram".
- Transforms value to uppercase using map and prints it if present.
- Chains multiple maps: first to uppercase, then to string length, and prints the result if present.
- getName(int id) always returns Optional.of("ram").
Why
- Illustrates how Optional supports transformation without explicit null checks.
- Encourages developers to use functional operations (map, ifPresent) instead of imperative style.
- Demonstrates safe chaining of transformations in a null-safe context.
- Builds on earlier Optional example by showing multiple sequential transformations.
How
- Declared getName(int id) to return Optional<String>.
- In main, assigned result to Optional<String> optional.
- Applied optional.map(x -> x.toUpperCase()) to transform the contained string.
- Stored in optionalUpper and used ifPresent(System.out::println) for printing.
- Applied chained maps: first uppercase, then length calculation.
- Stored in optionalLength and printed via ifPresent.
Logic
- Inputs: integer id (in example, 2).
- Outputs:
1. "RAM"
2. 3
- Flow:
1. Call getName(2) → returns Optional.of("ram").
2. Apply map to convert to uppercase: "RAM".
3. Print result through ifPresent.
4. Apply chained maps: "ram" → "RAM" → 3.
5. Print result through ifPresent.
- Edge cases handled:
- If getName returned Optional.empty(), maps would propagate emptiness without errors and nothing would be printed.
- No risk of NullPointerException; operations are safe.
- Complexity / performance: O(1) for each map operation; negligible overhead.
- Concurrency / thread-safety:
- Optional and string are immutable.
- Safe across threads.
- Error handling:
- No exceptions unless lambda itself throws, which it does not here.
Real-life applications
- Safe value transformation pipelines when values may be absent.
- Example for functional programming teaching in Java.
- Useful in data processing flows where null checks are undesirable.
- Provides a template for applying multiple dependent transformations safely.
Notes
- getName currently always returns non-empty Optional; could be expanded to return Optional.empty() for missing cases.
- For readability, method references (String::toUpperCase, String::length) can replace lambdas.
- Chaining encourages clear and concise handling of optional data.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 56addfd commit ecc52a4
File tree
1 file changed
+22
-0
lines changed- Java 8 Crash Course/Lambda Expression/Optional/src/Package1
1 file changed
+22
-0
lines changedLines changed: 22 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 | + | |
0 commit comments