Skip to content

Commit 3ca0b1d

Browse files
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

File tree

JAVA8/Predicates/src/PredicateDemo.java renamed to Java 8 Crash Course/Predicate/src/PredicateDemo.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,37 @@
22

33
public class PredicateDemo {
44
public static void main(String[] args) {
5-
// Predicate --> Functional interface ( Boolean valued function )
6-
5+
// Predicate --> Functional interface & it is a Boolean valued function.
76
Predicate<Integer> isEven = x -> x % 2 == 0;
7+
88
//Kutch check karna hai toh predicate use karna.Bas Ek condition hold karta hai.
99
//ek condition ko variable store mai store karta hai.
1010
System.out.println(isEven.test(4));
1111

12-
Predicate<String> isWordStartingWithLetterA = x-> x.startsWith("A");
13-
System.out.println(isWordStartingWithLetterA.test("Ankit"));
14-
12+
Predicate<String> isWordStartingWithLetterA = x -> x.startsWith("S");
13+
System.out.println(isWordStartingWithLetterA.test("Stark"));
1514

1615
//combining a predicate.
17-
Predicate<String> isWordStartingWithLetterZ = x-> x.startsWith("Z");
18-
Predicate<String> isWordEndingWithA = x-> x.startsWith("A");
16+
Predicate<String> isWordStartingWithLetterZ = x -> x.startsWith("Z");
17+
Predicate<String> isWordEndingWithA = x -> x.startsWith("A");
1918
Predicate<String> Check = isWordEndingWithA.and(isWordEndingWithA);
2019

2120
System.out.println(Check.test("AoooooZ"));
2221

2322
Predicate<String> isWordStartingWithA = x -> x.toLowerCase().startsWith("a");
2423
Predicate<String> isWordEndingWithT = x -> x.toLowerCase().endsWith("t");
2524
Predicate<String> and = isWordStartingWithA.and(isWordEndingWithT);
26-
System.out.println(and.test("Akshay"));
25+
System.out.println(and.test("Jackson"));
26+
27+
28+
Predicate<Integer> p = x -> x >= 1000000;
29+
System.out.println(p.test(1000000));
30+
31+
int salary = 90;
32+
if (p.test(salary)) {
33+
System.out.println("True");
34+
} else {
35+
System.out.println("False");
36+
}
2737
}
2838
}

0 commit comments

Comments
 (0)