Skip to content

Commit 7b2a438

Browse files
committed
feat(OptionalDemo): add demo using map, filter, and orElse with Optional
What - Added OptionalDemo class demonstrating core Optional operations. - Creates an Optional containing "hello". - Applies the following transformations: - map(String::toUpperCase) → "HELLO". - filter(s -> s.startsWith("H")) → value passes filter and remains. - orElse("world") → fallback used only if empty; here value remains "HELLO". - Prints final result to stdout. Why - Showcases how Optional can be used in a fluent, functional style. - Demonstrates chaining of map, filter, and orElse to safely transform and consume values. - Provides educational example for developers learning Optional API idioms. How - Constructed Optional<String> using Optional.of("hello"). - Applied map to convert string to uppercase. - Applied filter to retain value only if condition matches. - Used orElse to provide fallback in case of empty Optional. - Printed final result. Logic - Inputs: none (hardcoded value "hello"). - Outputs: "HELLO" printed to stdout. - Flow: 1. Start with Optional.of("hello"). 2. map(String::toUpperCase) → Optional.of("HELLO"). 3. filter(s -> s.startsWith("H")) → condition true, remains Optional.of("HELLO"). 4. orElse("world") → since value present, returns "HELLO". 5. Print result. - Edge cases handled: - If initial value did not start with "H", filter would make Optional empty, and orElse would return "world". - No null risks due to Optional. - Complexity / performance: O(1) per operation, negligible cost. - Concurrency / thread-safety: - Optional and string values are immutable. - Safe across threads. - Error handling: - No exceptions expected. - orElse guarantees fallback for empty cases. Real-life applications - Data sanitization and conditional validation pipelines. - Applying transformations with safe defaults. - Avoiding verbose null-checks in business logic. - Useful for input validation (e.g., checking prefixes, applying case normalization). Notes - This example demonstrates fluent chaining in Optional. - orElse eagerly evaluates argument; for expensive defaults, use orElseGet. - Method references improve readability (e.g., String::toUpperCase). - Optional should primarily be used for return values, not fields or serialization. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 409fe8d commit 7b2a438

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import java.util.Optional; // Import the Optional class
2+
3+
public class OptionalDemo {
4+
public static void main(String[] args) {
5+
// Create an Optional containing the string "hello"
6+
Optional<String> optional = Optional.of("hello");
7+
8+
// Transform the value inside the Optional to uppercase ("HELLO")
9+
String result = optional
10+
.map(String::toUpperCase)
11+
// Filter the value - check if it starts with "H". "HELLO" starts with "H", so the Optional remains present.
12+
.filter(s -> s.startsWith("H"))
13+
// If the Optional was empty, return "world". It's not empty, so return the value inside ("HELLO").
14+
.orElse("world");
15+
System.out.println(result);
16+
}
17+
}

0 commit comments

Comments
 (0)