Skip to content

Commit 5315371

Browse files
committed
feat(functional-interfaces): demonstrate Function, Predicate, Consumer, and Supplier usage
What - Added FunctionalInterfaceExample class showcasing four core functional interfaces from `java.util.function`: 1. Function<T,R> → transforms input to output. - Example: String → Integer (length). 2. Predicate<T> → evaluates condition, returns boolean. - Example: check if an integer is even. 3. Consumer<T> → consumes input, no return. - Example: prints a message. 4. Supplier<T> → supplies a value without input. - Example: generates random numbers. Why - Functional interfaces are the foundation of Java 8’s functional programming. - Provides concrete examples for commonly used built-in interfaces. - Clarifies how lambdas map directly to single abstract methods. Logic 1. Function<String,Integer> stringLength = str -> str.length(); - Calls `apply("Java8")`, prints length = 5. 2. Predicate<Integer> isEven = n -> n % 2 == 0; - Tests with 10 (true) and 7 (false). 3. Consumer<String> printMessage = msg -> System.out.println(msg); - Accepts and prints a custom string. 4. Supplier<Double> randomSupplier = () -> Math.random(); - Generates and supplies a random double value. Real-life applications - Function<T,R>: data transformation (DTO mapping, stream map operations). - Predicate<T>: filtering collections (stream filters, input validation). - Consumer<T>: logging, notifications, UI rendering actions. - Supplier<T>: lazy initialization, providing IDs, timestamps, or random values. Notes - Each of these interfaces has exactly one abstract method, making them functional interfaces. - Can be composed or combined with methods like `andThen`, `or`, `negate` for more complex logic. - Central to Java Streams API and event-driven programming. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent d85c515 commit 5315371

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import java.util.function.Function;
2+
import java.util.function.Predicate;
3+
import java.util.function.Consumer;
4+
import java.util.function.Supplier;
5+
6+
public class FunctionalInterfaceExample {
7+
public static void main(String[] args) {
8+
// 1. Function<T, R>
9+
// Takes an input of type T, returns an output of type R
10+
// Example: Convert String -> Integer (length of string)
11+
12+
Function<String, Integer> stringLength = str -> str.length();
13+
System.out.println("Function Example: Length of 'Java8' = " + stringLength.apply("Java8"));
14+
15+
// 2. Predicate<T>
16+
// Takes an input of type T, returns boolean
17+
// Example: Check if a number is even
18+
Predicate<Integer> isEven = n -> n % 2 == 0;
19+
System.out.println("Predicate Example: Is 10 even? " + isEven.test(10));
20+
System.out.println("Predicate Example: Is 7 even? " + isEven.test(7));
21+
22+
// 3. Consumer<T>
23+
// Takes an input of type T, returns nothing (just consumes it)
24+
// Example: Print a message to console
25+
Consumer<String> printMessage = msg -> System.out.println("Consumer Example: " + msg);
26+
printMessage.accept("Hello from Consumer!");
27+
28+
// 4. Supplier<T>
29+
// Takes no input, only supplies a value of type T
30+
// Example: Supply a random number
31+
Supplier<Double> randomSupplier = () -> Math.random();
32+
System.out.println("Supplier Example: Random number = " + randomSupplier.get());
33+
34+
/*
35+
Quick Recap (Why Functional Interfaces are powerful)
36+
- Function → Transform data
37+
- Predicate → Filter data (boolean logic)
38+
- Consumer → Perform action (like printing, logging)
39+
- Supplier → Provide data (like random numbers, IDs, timestamps)
40+
41+
• Function = input → output
42+
• Predicate = test condition → boolean
43+
• Consumer = just use input, no return
44+
• Supplier = only return, no input
45+
*/
46+
}
47+
}

0 commit comments

Comments
 (0)