Skip to content

Commit 32a0999

Browse files
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 changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import java.util.function.Predicate;
2+
3+
public class Main {
4+
public static void main(String[] args) {
5+
Predicate<String> startsWithLetterV = x -> x.toLowerCase().charAt(0) == 'v';
6+
7+
Predicate<String> endsWithLetterL = x -> x.toLowerCase().charAt(x.length() - 1) == 'l';
8+
9+
Predicate<String> and = startsWithLetterV.and(endsWithLetterL);
10+
11+
Predicate<String> or = startsWithLetterV.or(endsWithLetterL);
12+
13+
Predicate<String> startsWithV = x -> x.startsWith("V");
14+
15+
16+
System.out.println("AND Test with 'VIP' → " + and.test("VIP"));
17+
System.out.println("OR Test with 'Vivek' → " + or.test("Vivek"));
18+
19+
// Negate the predicate.
20+
Predicate<String> negate = startsWithV.negate();
21+
System.out.println("Negate Test with 'VIP' → " + negate.test("VIP"));
22+
System.out.println("Negate Test with 'ABC' → " + negate.test("ABC"));
23+
}
24+
}

0 commit comments

Comments
 (0)