Commit 47dd7ab
committed
feat(Package): add Main demonstrating Predicate with strings and custom Student class
What
- Added Main class in package Package to demonstrate use of java.util.function.Predicate.
- Defined:
- Predicate<String> startsWithLetterV → checks if string starts with 'v' (case-insensitive).
- Predicate<String> endsWithLetterL → checks if string ends with 'l' (case-insensitive).
- Predicate<Student> studentPredicate → checks if Student id > 1.
- Created two Student objects: ("Jack", 1) and ("Jackson", 2).
- Evaluated studentPredicate on s2 and printed result (true).
Why
- Demonstrates how Predicate can be applied to both simple data types (String) and custom objects.
- Provides an example of functional-style conditional checks replacing verbose if-statements.
- Encourages developers to use Predicate for clean, reusable validation logic.
How
- Declared lambda-based Predicates for string and custom type.
- Created Student inner class with name, id, getters, setters, and constructor.
- Tested studentPredicate on Student instance with id=2.
- Printed output to console.
Logic
- Inputs:
- Strings (for demonstration predicates).
- Student objects (id = 1 and id = 2).
- Outputs:
- Printed true when testing studentPredicate on Student with id=2.
- Flow:
1. Create string predicates (not used in output but valid examples).
2. Instantiate Student s1 and s2.
3. Define studentPredicate to check id > 1.
4. Test predicate with s2 (id=2) → returns true.
5. Print result.
- Edge cases:
- String predicates assume non-empty input (toLowerCase().charAt(0)).
- Would throw StringIndexOutOfBoundsException for empty strings.
- Student predicate works only if id is initialized properly.
- Complexity / performance: O(1) operations.
- Concurrency / thread-safety:
- Stateless predicates, safe across threads.
- Error handling:
- No checked exceptions, but empty string inputs would cause runtime error in string predicates.
Real-life applications
- Using Predicates for data validation (e.g., checking IDs, names, formats).
- Filtering lists of objects with stream().filter(predicate).
- Defining composable business rules (Predicates support and(), or(), negate()).
- Helpful in frameworks for authorization, filtering, or event handling logic.
Notes
- Demonstrates inner static Student class for encapsulation; can be extracted to top-level class in larger projects.
- Predicates are composable: e.g., startsWithLetterV.and(endsWithLetterL).
- Good template for validating entities in functional pipelines.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent c37590e commit 47dd7ab
File tree
1 file changed
+43
-0
lines changed- Java 8 Crash Course/Predicate/Default Static Methods Inside Predicate Interface/src/Package
1 file changed
+43
-0
lines changedLines changed: 43 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 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
0 commit comments