0% found this document useful (0 votes)
2 views

Functional_Interfaces_Java_In_Depth

Functional interfaces in Java are interfaces with exactly one abstract method, primarily used for lambda expressions and method references. They promote cleaner code, functional programming, and are integral to the Streams API and other Java features. Common built-in functional interfaces include Predicate, Function, Consumer, Supplier, UnaryOperator, and BinaryOperator, which facilitate various programming tasks such as filtering, transformation, and action execution.

Uploaded by

msrbharath
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Functional_Interfaces_Java_In_Depth

Functional interfaces in Java are interfaces with exactly one abstract method, primarily used for lambda expressions and method references. They promote cleaner code, functional programming, and are integral to the Streams API and other Java features. Common built-in functional interfaces include Predicate, Function, Consumer, Supplier, UnaryOperator, and BinaryOperator, which facilitate various programming tasks such as filtering, transformation, and action execution.

Uploaded by

msrbharath
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Functional Interfaces in Java – In Depth

1. What is a Functional Interface?


A functional interface is an interface with exactly one abstract method.
It may contain multiple default or static methods, but only one abstract method.

It is used primarily for **lambda expressions** and **method references** in Java 8 and
above.

Example:
@FunctionalInterface
public interface MyFunction {
void execute();
}

2. Purpose of Functional Interfaces


- Enable use of **lambda expressions** for cleaner, more expressive code.
- Encourage functional programming paradigms.
- Promote reusability and testability.
- Used extensively in **Streams API**, **CompletableFuture**, and **custom logic
injection**.

3. Common Built-in Functional Interfaces


1. **Predicate<T>** – Accepts T, returns boolean
- Used for filtering
- Example: `stream.filter(s -> s.length() > 3)`

2. **Function<T, R>** – Accepts T, returns R


- Used for transformation
- Example: `stream.map(s -> s.toUpperCase())`

3. **Consumer<T>** – Accepts T, returns nothing


- Used for performing actions
- Example: `stream.forEach(System.out::println)`

4. **Supplier<T>** – Accepts nothing, returns T


- Used for generating or fetching values
- Example: `Supplier<String> supplier = () -> "Hello"`

5. **UnaryOperator<T>** – Like Function<T, T>


- Used for transforming values of same type
6. **BinaryOperator<T>** – Accepts (T, T), returns T
- Used for reductions (e.g., summing values)

4. Custom Functional Interface Example


@FunctionalInterface
public interface Calculator {
int compute(int a, int b);
}

Usage with lambda:


Calculator add = (a, b) -> a + b;
System.out.println(add.compute(5, 3)); // 8

5. Real-World Use Cases and Scenarios


✅ Filtering lists with Predicate:
List<String> names = Arrays.asList("John", "Jane", "Alice");
names.stream().filter(name -> name.startsWith("J")).forEach(System.out::println);

✅ Data transformation with Function:


Function<String, Integer> parseLength = String::length;
System.out.println(parseLength.apply("Java")); // 4

✅ Logging with Consumer:


Consumer<String> logger = msg -> System.out.println("Log: " + msg);
logger.accept("Hello world");

✅ Caching with Supplier:


Supplier<Connection> connectionSupplier = () -> DriverManager.getConnection(...);

✅ Arithmetic with BinaryOperator:


BinaryOperator<Integer> sum = Integer::sum;
System.out.println(sum.apply(5, 10)); // 15

6. Functional Interfaces vs Normal Interfaces


| Feature | Functional Interface | Regular Interface |
|-------------------------|---------------------------|------------------------|
| Abstract methods | Exactly 1 | One or more |
| Lambda compatible | ✅ Yes | ❌ No |
| Purpose | Functional logic injection| General abstraction |
| Annotation (`@FunctionalInterface`) | Optional but recommended | Not applicable |

7. Summary
- Functional interfaces are foundational to Java's support for functional programming.
- Enable concise lambda expressions.
- Core to Stream API, method chaining, and custom functional logic injection.
- Can define your own or use built-in interfaces in java.util.function package.

You might also like