Commit cf2aee3
committed
feat(FunctionalProgramming): add Main3 demonstrating Predicate with String condition
What
- Added Main3 class in package FunctionalProgramming.
- Demonstrates Predicate<String> with lambda:
- startsWithLetterV → true if first character is 'v' (case-insensitive).
- Tests predicate on string "Jack" and prints result (false).
Why
- Shows how lambdas can be stored as variables representing functions.
- Illustrates functional programming concept: functions as first-class citizens in Java.
- Demonstrates applying Predicate to strings rather than numbers.
How
- Defined Predicate<String> startsWithLetterV = x -> x.toLowerCase().charAt(0) == 'v'.
- Called startsWithLetterV.test("Jack").
- Printed result to console.
Logic
- Input: string "Jack".
- Output: false.
- Flow:
1. Input string "Jack".
2. Convert to lowercase → "jack".
3. Take first character → 'j'.
4. Compare with 'v' → false.
5. Print false.
- Edge cases:
- Empty string would cause StringIndexOutOfBoundsException at charAt(0).
- Null input would cause NullPointerException.
- Complexity / performance: O(1) operations.
- Concurrency / thread-safety: Predicate is stateless, thread-safe.
- Error handling: Not present; unsafe for null or empty input.
Real-life applications
- Validating user input (e.g., checking prefixes of strings).
- Filtering lists of names in streams with startsWithLetterV predicate.
- Reusable predicates for business logic (prefix/suffix conditions).
Notes
- Comment notes correctly describe that lambdas represent functions stored in variables.
- This can be extended by combining predicates with and(), or(), negate().
- For production-safe code, add null/empty checks before charAt(0).
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent e9a5429 commit cf2aee3
File tree
1 file changed
+14
-0
lines changed- Java 8 Crash Course/Predicate/src/FunctionalProgramming
1 file changed
+14
-0
lines changedLines changed: 14 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 | + | |
0 commit comments