Skip to content

Commit ecc52a4

Browse files
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 changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package Package1;
2+
3+
import java.util.Optional;
4+
5+
public class Test {
6+
public static void main(String[] args) {
7+
Optional<String> optional = getName(2);
8+
9+
// Convert to uppercase and print.
10+
Optional<String> optionalUpper = optional.map(x -> x.toUpperCase());
11+
optionalUpper.ifPresent(System.out::println);
12+
13+
// Convert to length (Integer).
14+
Optional<Integer> optionalLength = optional.map(x -> x.toUpperCase())
15+
.map(x -> x.length());
16+
optionalLength.ifPresent(System.out::println);
17+
}
18+
19+
private static Optional<String> getName(int id) {
20+
return Optional.of("ram");
21+
}
22+
}

0 commit comments

Comments
 (0)