Is lesson me hum seekhenge:
- Functional Programming kya hota hai
- Imperative vs Functional style
- Lambda Expressions
- Functional Interfaces
- Streams API
- Real examples
Functional Programming ka matlab:
code ko functions ke form me likhna
✔ less code
✔ clean code
✔ readable code
int sum = 0;
for(int i = 1; i <= 5; i++){
sum += i;
}int sum = IntStream.rangeClosed(1,5).sum();Lambda ek short way hai function likhne ka:
(parameters) -> expression
Example:
(a, b) -> a + bFunctional Interface:
jisme sirf ek abstract method hota hai
Example:
interface MyInterface {
void display();
}Lambda use:
MyInterface obj = () -> {
System.out.println("Hello");
};| Interface | Use |
|---|---|
| Predicate | condition check |
| Function | input → output |
| Consumer | accept value |
| Supplier | value provide |
Predicate<Integer> isEven = n -> n % 2 == 0;
System.out.println(isEven.test(4));Function<Integer, Integer> square = x -> x * x;
System.out.println(square.apply(5));Consumer<String> print = s -> System.out.println(s);
print.accept("Hello");Supplier<Integer> random = () -> 100;
System.out.println(random.get());Streams use hota hai:
data processing ke liye (collection par)
import java.util.*;
class Test {
public static void main(String[] args){
List<Integer> list = Arrays.asList(1,2,3,4,5);
list.stream()
.filter(n -> n % 2 == 0)
.forEach(System.out::println);
}
}list.stream()
.map(n -> n * n)
.forEach(System.out::println);int sum = list.stream()
.reduce(0, (a, b) -> a + b);
System.out.println(sum);Short form:
System.out::println✔ less code
✔ better readability
✔ parallel processing possible
filter → even numbers
map → square
reduce → sum
✔ Java 8 se introduce hua
✔ Lambda + Stream important hai
✔ functional style modern coding hai
- Functional Programming kya hota hai?
- Lambda kya hota hai?
- Functional interface kya hota hai?
- Stream API ka use kya hai?
Is lesson me humne seekha:
✔ Functional Programming concept
✔ Lambda Expressions
✔ Functional Interfaces
✔ Streams API
✔ Real examples
Functional Programming Java me clean, concise aur powerful coding style provide karta hai.