Commit a4936a9
committed
feat(functional-interface): add Function demo for String length and student filtering by prefix
What
- Added `Main` class demonstrating multiple uses of `Function<T,R>`:
1. `Function<String,Integer>` → calculates string length (`"Jackson"` → 7).
2. `Function<String,String>` → extracts substring (first 3 characters).
3. `Function<List<Student>, List<Student>>` → custom function to filter students whose names start with `"vip"` (case-insensitive).
- Introduced inner `Student` class with fields `id` and `name`, along with getters, setters, and overridden `toString()`.
Why
- Shows versatility of the `Function` interface:
- Simple transformations (`String → Integer`).
- Substring extraction (`String → String`).
- Business logic functions (`List<Student> → List<Student>`).
- Highlights functional programming approach where behavior is encapsulated in reusable, composable functions.
- Demonstrates filtering logic without external loops in main method, improving readability.
Logic
1. **String Length Function**
- `Function<String,Integer> function = x -> x.length();`
- `function.apply("Jackson")` → returns `7`.
2. **Substring Function**
- `Function<String,String> function2 = s -> s.substring(0,3);`
- Applied to any string to get the first three characters.
3. **Student Filtering Function**
- Iterates over input `List<Student>`.
- Checks: `s.getName() != null && s.getName().toLowerCase().startsWith("vip")`.
- If condition passes → adds to result list.
- Applied on a list of students:
- Students:
`2 - VIP`
`3 - Jackson`
`4 - Stark`
- Output: `[2 - VIP]` (only student with `"vip"` prefix).
Real-life applications
- String transformation pipelines (e.g., trimming, validation, encoding).
- Filtering domain objects (e.g., students, employees, products) based on dynamic business rules.
- Encapsulating data transformation logic into reusable `Function` objects for cleaner APIs.
- Can be combined with Java Streams (`stream().map(...).filter(...)`) for declarative data processing.
Notes
- `Function<T,R>` encourages separation of transformation logic from execution.
- Use `.andThen()` or `.compose()` to chain multiple `Function` objects for complex transformations.
- Inner `Student` class kept simple for demo purposes; in production, prefer separate class files with immutability or records (Java 14+).
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 7342ceb commit a4936a9
1 file changed
+64
-0
lines changed| 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 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
0 commit comments