Skip to content

Commit cf2aee3

Browse files
committed
feat(FunctionalProgramming): add Main3 demonstrating Predicate with String condition
What - Added Main3 class in package FunctionalProgramming. - Demonstrates Predicate<String> with lambda: - startsWithLetterV → true if first character is 'v' (case-insensitive). - Tests predicate on string "Jack" and prints result (false). Why - Shows how lambdas can be stored as variables representing functions. - Illustrates functional programming concept: functions as first-class citizens in Java. - Demonstrates applying Predicate to strings rather than numbers. How - Defined Predicate<String> startsWithLetterV = x -> x.toLowerCase().charAt(0) == 'v'. - Called startsWithLetterV.test("Jack"). - Printed result to console. Logic - Input: string "Jack". - Output: false. - Flow: 1. Input string "Jack". 2. Convert to lowercase → "jack". 3. Take first character → 'j'. 4. Compare with 'v' → false. 5. Print false. - Edge cases: - Empty string would cause StringIndexOutOfBoundsException at charAt(0). - Null input would cause NullPointerException. - Complexity / performance: O(1) operations. - Concurrency / thread-safety: Predicate is stateless, thread-safe. - Error handling: Not present; unsafe for null or empty input. Real-life applications - Validating user input (e.g., checking prefixes of strings). - Filtering lists of names in streams with startsWithLetterV predicate. - Reusable predicates for business logic (prefix/suffix conditions). Notes - Comment notes correctly describe that lambdas represent functions stored in variables. - This can be extended by combining predicates with and(), or(), negate(). - For production-safe code, add null/empty checks before charAt(0). Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent e9a5429 commit cf2aee3

File tree

1 file changed

+14
-0
lines changed
  • Java 8 Crash Course/Predicate/src/FunctionalProgramming

1 file changed

+14
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package FunctionalProgramming;
2+
3+
import java.util.function.Predicate;
4+
5+
public class Main3 {
6+
public static void main(String[] args) {
7+
// Predicate<Integer> isEven = x % 2 == 0;
8+
// Lambda Expression and We can also say that is Function.
9+
// We are storing a function as a Variable.
10+
11+
Predicate<String> startsWithLetterV = x -> x.toLowerCase().charAt(0) == 'v';
12+
System.out.println(startsWithLetterV.test("Jack"));
13+
}
14+
}

0 commit comments

Comments
 (0)