Commit 32a0999
committed
feat(PredicateDemo): add Main demonstrating and(), or(), and negate() with Predicates
What
- Added Main class showcasing advanced usage of java.util.function.Predicate.
- Defined multiple Predicates:
- startsWithLetterV → true if string starts with 'v' (case-insensitive).
- endsWithLetterL → true if string ends with 'l' (case-insensitive).
- and → composition of startsWithLetterV.and(endsWithLetterL).
- or → composition of startsWithLetterV.or(endsWithLetterL).
- startsWithV → true if string starts with uppercase "V".
- negate → logical negation of startsWithV.
- Printed results of predicate evaluations with test strings.
Why
- Demonstrates how to combine and manipulate predicates using built-in logical methods (and, or, negate).
- Encourages declarative validation logic rather than imperative if-else chains.
- Serves as an educational example of functional composition in Java.
How
- Created base predicates with lambda expressions.
- Built composite predicates using and() and or().
- Applied negate() to invert the logic of startsWithV.
- Printed results for different test cases ("VIP", "Vivek", "ABC").
Logic
- Inputs: hardcoded strings ("VIP", "Vivek", "ABC").
- Outputs:
- "AND Test with 'VIP' → false" (VIP does not end with 'l').
- "OR Test with 'Vivek' → true" (starts with 'v').
- "Negate Test with 'VIP' → false" (VIP starts with V, negated → false).
- "Negate Test with 'ABC' → true" (ABC does not start with V, negated → true).
- Flow:
1. Build simple predicates.
2. Combine them with and/or.
3. Negate one predicate for inversion.
4. Test results and print outputs.
- Edge cases:
- Predicates assume non-empty strings; empty string would cause StringIndexOutOfBoundsException in charAt().
- Complexity / performance: O(1) checks per test, negligible cost.
- Concurrency / thread-safety: Predicates are stateless, fully thread-safe.
- Error handling: No exceptions unless empty string provided.
Real-life applications
- Filtering data in streams with compound conditions.
- Validation pipelines for user input or configuration values.
- Dynamic rule composition for authorization, access control, or event handling.
- Building reusable condition sets with negate/or/and for flexibility.
Notes
- Use method references where possible for clarity (e.g., String::startsWith for simple cases).
- Predicate chaining allows modular conditions without nested if-statements.
- For more complex rules, predicates can be stored in collections and combined dynamically.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 47dd7ab commit 32a0999
File tree
1 file changed
+24
-0
lines changed- Java 8 Crash Course/Predicate/Default Static Methods Inside Predicate Interface/src
1 file changed
+24
-0
lines changedLines changed: 24 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 | + | |
| 23 | + | |
| 24 | + | |
0 commit comments