Skip to content

Commit a4936a9

Browse files
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

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import java.util.ArrayList;
2+
import java.util.Arrays;
3+
import java.util.List;
4+
import java.util.function.Function;
5+
6+
public class Main {
7+
public static void main(String[] args) {
8+
Function<String, Integer> function = x -> x.length();
9+
System.out.println(function.apply("Jackson"));
10+
11+
Function<String, String> function2 = s -> s.substring(0,3);
12+
System.out.println();
13+
14+
Function<List<Student>, List<Student>> studentsWithVipAsPrefix = li -> {
15+
List<Student> result = new ArrayList<>();
16+
17+
for(Student s : li) {
18+
if (s.getName() != null && s.getName().toLowerCase().startsWith("vip")) {
19+
result.add(s);
20+
}
21+
}
22+
return result;
23+
};
24+
Student s1 = new Student(2,"VIP");
25+
Student s2 = new Student(3,"Jackson");
26+
Student s3 = new Student(4,"Stark");
27+
28+
List<Student> students = Arrays.asList(s1, s2, s3);
29+
30+
List<Student> filteredStudents = studentsWithVipAsPrefix.apply(students);
31+
System.out.println(filteredStudents);
32+
}
33+
34+
private static class Student {
35+
private int id;
36+
private String name;
37+
38+
public Student(int id, String name) {
39+
this.id = id;
40+
this.name = name;
41+
}
42+
43+
public int getId() {
44+
return id;
45+
}
46+
47+
public void setId(int id) {
48+
this.id = id;
49+
}
50+
51+
public String getName() {
52+
return name;
53+
}
54+
55+
public void setName(String name) {
56+
this.name = name;
57+
}
58+
59+
@Override
60+
public String toString() {
61+
return id + " - " + name;
62+
}
63+
}
64+
}

0 commit comments

Comments
 (0)