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

Functional Programming in Java

Functional programming in Java utilizes functional interfaces and lambda expressions to enhance code conciseness and reusability. Key functional interfaces include Function, BiFunction, Predicate, Supplier, and Consumer, each serving distinct purposes in data manipulation and operations. Lambda expressions allow for anonymous function creation, enabling cleaner syntax and the ability to pass functions as arguments, while also supporting features like method references and exception handling.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Functional Programming in Java

Functional programming in Java utilizes functional interfaces and lambda expressions to enhance code conciseness and reusability. Key functional interfaces include Function, BiFunction, Predicate, Supplier, and Consumer, each serving distinct purposes in data manipulation and operations. Lambda expressions allow for anonymous function creation, enabling cleaner syntax and the ability to pass functions as arguments, while also supporting features like method references and exception handling.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Functional Programming in Java

Functional programming in Java revolves around functional interfaces and


lambda expressions to create concise, readable, and reusable code.

1️⃣ Functional Interfaces in Java

A functional interface is an interface that contains exactly one abstract


method. It can have multiple default and static methods.

📌 Key Functional Interfaces in Java:

1. Function<T, R> → Takes one argument of type T, returns R.


2. BiFunction<T, U, R> → Takes two arguments T, U, returns R.
3. Predicate<T> → Takes T, returns boolean (used for conditions).
4. Supplier<T> → Takes nothing, returns T.
5. Consumer<T> → Takes T, returns nothing (used for actions).

2️⃣ Lambda Expressions Fundamentals

A lambda expression is an anonymous function that can be assigned to a


functional interface.

📌 Syntax:

java

(parameter1, parameter2) -> { return expression; }

📌 Example:

java

Function<Integer, Integer> square = x -> x * x;


System.out.println(square.apply(5)); // Output: 25

3️⃣ Functional Interfaces & Lambda Examples

3.1 Function<T, R> (One Argument, One Return Value)


📌 Use: Transforms data (like map in functional programming).

java

import java.util.function.Function;

public class FunctionExample {


public static void main(String[] args) {
Function<String, Integer> lengthFunction =
str -> str.length();

System.out.println(lengthFunction.apply("Hello")); /
/ Output: 5
}
}

3.2 BiFunction<T, U, R> (Two Arguments, One Return Value)

📌 Use: Operations that need two inputs.

java

import java.util.function.BiFunction;

public class BiFunctionExample {


public static void main(String[] args) {
BiFunction<Integer, Integer, Integer> add =
(a, b) -> a + b;
System.out.println(add.apply(10, 20)); //
Output: 30
}
}

3.3 Predicate<T> (Returns Boolean)

📌 Use: Used for filtering and conditions.

java

import java.util.function.Predicate;

public class PredicateExample {


public static void main(String[] args) {
Predicate<Integer> isEven = num -> num % 2 ==
0;
System.out.println(isEven.test(8)); //
Output: true
}
}

3.4 Supplier<T> (No Input, Returns a Value)

📌 Use: Provides values on demand (e.g., getting system time, generating


random numbers).

java

import java.util.function.Supplier;

public class SupplierExample {


public static void main(String[] args) {
Supplier<Double> randomNumber = () ->
Math.random();
System.out.println(randomNumber.get()); //
Output: Random value
}
}

4️⃣ Block Lambda Expressions

A block lambda contains multiple statements inside {}.

java

Function<Integer, Integer> factorial = num -> {


int result = 1;
for (int i = 1; i <= num; i++) {
result *= i;
}
return result;
};

System.out.println(factorial.apply(5)); // Output:
120
5️⃣ Passing Lambda Expressions as Arguments

📌 Use: Higher-order functions (functions that take functions as arguments).

java

import java.util.function.Function;

public class LambdaAsArgument {


static int operate(int num, Function<Integer,
Integer> function) {
return function.apply(num);
}

public static void main(String[] args) {


System.out.println(operate(10, x -> x * x));
// Output: 100
}
}

6️⃣ Lambda Expressions and Exceptions

📌 Use: Handling checked exceptions inside lambda expressions.

java

import java.util.function.Consumer;

public class LambdaExceptionExample {


public static void main(String[] args) {
Consumer<String> fileOpener = fileName -> {
try {
if (fileName.isEmpty()) {
throw new Exception("File name
cannot be empty!");
}
System.out.println("Opening file: " +
fileName);
} catch (Exception e) {
System.out.println("Error: " +
e.getMessage());
}
};
fileOpener.accept(""); // Output: Error:
File name cannot be empty!
}
}

7️⃣ Variable Capture (Effectively Final Variables in Lambdas)

📌 Use: Lambda expressions can access final or effectively final local variables.

java

public class VariableCaptureExample {


public static void main(String[] args) {
int num = 10; // Effectively final (not
modified)
Function<Integer, Integer> multiply = x -> x
* num;
System.out.println(multiply.apply(5)); //
Output: 50
}
}

⚠️num must not be modified after assignment; otherwise, compilation fails!

8️⃣ Method References (Shortcut for Lambda Expressions)

📌 Use: When a lambda only calls an existing method, method references can
be used.

8.1 Static Method Reference

java

import java.util.function.Function;

public class MethodReferenceExample {


static int square(int x) {
return x * x;
}

public static void main(String[] args) {


Function<Integer, Integer> func =
MethodReferenceExample::square;
System.out.println(func.apply(6)); //
Output: 36
}
}

8.2 Instance Method Reference

java

public class InstanceMethodRef {


public int add(int a, int b) {
return a + b;
}

public static void main(String[] args) {


InstanceMethodRef obj = new
InstanceMethodRef();
BiFunction<Integer, Integer, Integer> func =
obj::add;
System.out.println(func.apply(10, 20)); //
Output: 30
}
}

8.3 Constructor Reference

java

import java.util.function.Supplier;

class Person {
String name;

Person() {
name = "Rahul"; }
}

public class ConstructorRef {


public static void main(String[] args) {
Supplier<Person> supplier = Person::new;
System.out.println(supplier.get().name); //
Output: Rahul
}
}

🎯 Final Summary

Feature Description Example


One input, one Function<Integer, String> f =
Function<T, R> x -> "Value: " + x;
output
BiFunction<Integer, Integer,
BiFunction<T, Two inputs, one Integer> add = (a, b) -> a +
U, R> output b;
Predicate<Integer> isEven = x
Predicate<T> Returns boolean -> x % 2 == 0;
No input, Supplier<Double> random = () -
Supplier<T> > Math.random();
returns value
Lambda Anonymous (x, y) -> x + y
Expressions functions
Multiple { int sum = a + b; return sum;
Block Lambda }
statements
Exception Try-catch inside fileOpener.accept("");
Handling lambda
Variable Uses effectively Function<Integer, Integer> f =
Capture final variables x -> x * num;
Method Shortcut for Function<String, Integer> f =
Reference lambdas String::length;

You might also like