Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

readme.md

☕ Java Lesson – Lambda Expression in Java

Is lesson me hum seekhenge:

  • Lambda Expression kya hota hai
  • Syntax aur structure
  • Functional Interface ke saath use
  • Real examples
  • Advantages

1️⃣ Lambda Expression Kya Hota Hai?

Lambda Expression ek:

anonymous function (naam ke bina function)

Use hota hai:

short aur clean code likhne ke liye

2️⃣ Syntax

(parameters) -> expression

3️⃣ Basic Example

(a, b) -> a + b

4️⃣ Without Lambda (Old Way)

interface MyInterface {
    void show();
}

class Test {

    public static void main(String[] args){

        MyInterface obj = new MyInterface() {
            public void show(){
                System.out.println("Hello");
            }
        };

        obj.show();
    }
}

5️⃣ With Lambda (Modern Way)

MyInterface obj = () -> {
    System.out.println("Hello");
};

obj.show();

✔ short
✔ clean


6️⃣ Functional Interface Required

✔ Lambda sirf tab use hota hai jab:

interface me ek hi abstract method ho

7️⃣ Parameter Variations

No Parameter

() -> System.out.println("Hello");

Single Parameter

x -> x * x

Multiple Parameters

(a, b) -> a + b

8️⃣ With Return

(a, b) -> {
    return a + b;
}

9️⃣ With Single Line (Implicit Return)

(a, b) -> a + b

🔟 Lambda with Collection

import java.util.*;

class Test {

    public static void main(String[] args){

        List<Integer> list = Arrays.asList(1,2,3,4);

        list.forEach(n -> System.out.println(n));
    }
}

1️⃣1️⃣ Lambda with Comparator

Collections.sort(list, (a, b) -> b - a);

1️⃣2️⃣ Lambda with Thread

Thread t = new Thread(() -> {
    System.out.println("Thread running");
});

t.start();

1️⃣3️⃣ Advantages

✔ less code
✔ readable code
✔ functional programming support
✔ no boilerplate code


1️⃣4️⃣ Limitations

❌ multiple methods wale interface me use nahi hota
❌ complex logic me readability kam ho sakti hai


1️⃣5️⃣ Real-Life Example

event handling
sorting
filtering data

1️⃣6️⃣ Important Points

✔ Java 8 me introduce hua
✔ functional interface ke saath use hota hai
✔ method reference ka base hai


🎯 Interview Questions

  1. Lambda expression kya hota hai?
  2. Functional interface kya hota hai?
  3. Lambda vs anonymous class?
  4. lambda ka syntax kya hai?

🧾 Lesson Summary

Is lesson me humne seekha:

✔ Lambda Expression concept
✔ syntax aur usage
✔ functional interface relation
✔ real examples

Lambda Expression Java me modern aur concise coding style provide karta hai.