Skip to content

Commit 47dd7ab

Browse files
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 changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package Package;
2+
3+
import java.util.function.Predicate;
4+
5+
public class Main {
6+
public static void main(String[] args) {
7+
Predicate<String> startsWithLetterV = x -> x.toLowerCase().charAt(0) == 'v';
8+
9+
Predicate<String> endsWithLetterL = x -> x.toLowerCase().charAt(x.length() - 1) == 'l';
10+
11+
Student s1 = new Student("Jack", 1);
12+
Student s2 = new Student("Jackson", 2);
13+
14+
Predicate<Student> studentPredicate = x -> x.getId() > 1;
15+
System.out.println(studentPredicate.test(s2));
16+
}
17+
18+
private static class Student {
19+
String name;
20+
int id;
21+
22+
public String getName() {
23+
return name;
24+
}
25+
26+
public void setName(String name) {
27+
this.name = name;
28+
}
29+
30+
public int getId() {
31+
return id;
32+
}
33+
34+
public void setId(int id) {
35+
this.id = id;
36+
}
37+
38+
public Student(String name, int id) {
39+
this.name = name;
40+
this.id = id;
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)