Commit 3ca0b1d
committed
feat(PredicateDemo): add demo showcasing multiple Predicate use cases and combinations
What
- Added PredicateDemo class demonstrating usage of java.util.function.Predicate with integers and strings.
- Examples included:
- Predicate<Integer> isEven → checks divisibility by 2.
- Predicate<String> isWordStartingWithLetterA → checks startsWith("S").
- Predicate<String> isWordStartingWithLetterZ → checks startsWith("Z").
- Predicate<String> isWordEndingWithA → incorrectly reused startsWith("A") (typo).
- Combined predicate Check using and().
- Predicate<String> isWordStartingWithA (case-insensitive).
- Predicate<String> isWordEndingWithT (case-insensitive).
- Combined predicate and = isWordStartingWithA.and(isWordEndingWithT).
- Predicate<Integer> p → checks x >= 1,000,000.
- Printed results of multiple tests, including salary check.
Why
- Demonstrates functional programming with predicates for boolean-valued functions.
- Shows how predicates encapsulate conditions into reusable, composable variables.
- Highlights use of and() to combine multiple conditions.
- Practical educational example for conditions with strings, numbers, and logical composition.
How
- Declared predicates with lambda expressions.
- Used test() to evaluate conditions on sample values.
- Combined predicates using and().
- Printed boolean results for verification.
- Used predicate p to validate salary threshold and print conditional output.
Logic
- Inputs:
- Integers: 4, 1,000,000, 90.
- Strings: "Stark", "AoooooZ", "Jackson".
- Outputs:
- isEven.test(4) → true.
- isWordStartingWithLetterA.test("Stark") → true.
- Check.test("AoooooZ") → true (due to startsWith("A")).
- and.test("Jackson") → false.
- p.test(1,000,000) → true.
- salary = 90, p.test(90) → false → prints "False".
- Flow:
1. Define predicates for various string and numeric conditions.
2. Combine some predicates with and().
3. Evaluate predicates with sample inputs.
4. Print results.
5. Use predicate in if-else to branch logic.
- Edge cases:
- Some predicates incorrectly reuse startsWith where endsWith was intended.
- Empty/null strings would cause exceptions.
- Complexity / performance: O(1) checks, negligible.
- Concurrency / thread-safety: Predicates are stateless, safe across threads.
- Error handling: No explicit null/empty handling, may throw runtime errors.
Real-life applications
- Validating user inputs (e.g., names starting/ending with specific characters).
- Applying salary or threshold checks in business logic.
- Filtering collections in stream pipelines using predicates.
- Combining predicates for flexible business rules.
Notes
- Typo: isWordEndingWithA uses startsWith instead of endsWith.
- Predicates are composable with and(), or(), negate() for complex rules.
- For clarity, method references like String::startsWith could be used in simple cases.
Signed-off-by: https://github.com/Someshdiwan <someshdiwan369@gmail.com>1 parent cf2aee3 commit 3ca0b1d
1 file changed
Lines changed: 18 additions & 8 deletions
Lines changed: 18 additions & 8 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | | - | |
6 | | - | |
| 5 | + | |
7 | 6 | | |
| 7 | + | |
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
12 | | - | |
13 | | - | |
14 | | - | |
| 12 | + | |
| 13 | + | |
15 | 14 | | |
16 | 15 | | |
17 | | - | |
18 | | - | |
| 16 | + | |
| 17 | + | |
19 | 18 | | |
20 | 19 | | |
21 | 20 | | |
22 | 21 | | |
23 | 22 | | |
24 | 23 | | |
25 | 24 | | |
26 | | - | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
27 | 37 | | |
28 | 38 | | |
0 commit comments