← Back to Advanced Java Course | Chapter 3: Functional Programming | Lesson 1 of 6

Java के Lambda Expressions

एक lambda expression एक छोटा नामहीन function है जिसे आप एक line में लिखते हैं, जैसे एक quick sticky-note instruction। यह व्यवहार के छोटे हिस्सों के लिए सुविधाजनक है।
Syntax
java
(parameters) -> expression

(parameters) -> {
    // statements
    return value;
}

What is a Lambda Expression?

Lambda expressions anonymous, नामहीन functions हैं जो सिर्फ एक behavior को पास करने के लिए एक पूरी class लिखने की formalities हटाकर आपका code सरल बनाते हैं। वे आपको functionality के एक टुकड़े को एक method argument की तरह treat करने देते हैं, मूल रूप से code को खुद एक data मानते हुए जिसे आप pass कर सकते हैं।

उदाहरण: What is a Lambda Expression?

java
// Define the class Main
public class Main {
	// Define the interface Greeting
	interface Greeting {
		void greet();
	}
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		Greeting g = () -> System.out.println("Hello from a lambda");
		g.greet();
	}
}

Lambda with Parameters

आप किसी lambda expression को parameters, arrow (->) operator के बाईं ओर parentheses के अंदर declare करके पास कर सकते हैं, बिल्कुल किसी normal method की parameter list की तरह, सिवाय इसके कि Java आमतौर पर उनके types को अपने आप infer कर सकता है।

उदाहरण: Lambda with Parameters

java
// Define the class Main
public class Main {
	// Define the interface Adder
	interface Adder {
		int add(int a, int b);
	}
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		Adder adder = (a, b) -> a + b;
		// Print a line to the console
		System.out.println(adder.add(3, 4));
	}
}

Lambda with Return Values

अगर किसी lambda body में सिर्फ एक single expression हो, तो Java बिना किसी explicit return keyword के अपने आप उसकी value return कर देता है, one-liners को compact रखते हुए। multi-statement bodies के लिए, code को curly braces में wrap करें और बिल्कुल एक साधारण method की तरह एक explicit return statement लिखें।

उदाहरण: Lambda with Return Values

java
public class Main {
	interface Square {
		int of(int n);
	}
	public static void main(String[] args) {
		Square oneLiner = n -> n * n; // implicit return
		Square multiLine = n -> {
			int result = n * n;
			return result;
		};
		System.out.println(oneLiner.of(4) + " " + multiLine.of(5));
	}
}

Lambdas and Variable Scope

Lambda expressions अपने enclosing scope से instance या static variables को स्वतंत्र रूप से पढ़ सकते हैं, लेकिन वे सिर्फ उन local variables को capture कर सकते हैं जो final declare किए गए हों या effectively final हों, यानी initialization के बाद कभी reassign न हुए हों। यह प्रतिबंध इसलिए है क्योंकि lambda शायद बाद में, किसी अलग thread पर चले, जब original local variable अन्यथा scope से बाहर जा चुका होता।

उदाहरण: Lambdas and Variable Scope

java
public class Main {
	interface Printer {
		void print();
	}
	public static void main(String[] args) {
		final int count = 10; // effectively final, safe to capture
		Printer p = () -> System.out.println("Count is " + count);
		p.print();
	}
}

Common Use Cases

Lambda expressions सबसे आम तौर पर संक्षिप्त collection iteration, filtering predicates, या custom sorting comparators inline लिखने के लिए उपयोग होते हैं, ठीक वहां जहां उनकी ज़रूरत है, बजाय codebase में कहीं और बिखरी अलग named classes के।

उदाहरण: Common Use Cases

java
import java.util.*;
public class Main {
	public static void main(String[] args) {
		List<Integer> nums = new ArrayList<>(List.of(5, 3, 8, 1));
		nums.removeIf(n -> n < 4); // filtering predicate
		nums.sort((a, b) -> b - a); // custom comparator
		System.out.println(nums);
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. किसी lambda के अंदर से एक local variable को modify करना, जैसे count++, जो compile नहीं होता क्योंकि captured locals को final या effectively final होना चाहिए।
  2. किसी lambda का उपयोग करना जहां target type एक functional interface नहीं है, जैसे var f = () -> 5; या दो abstract methods वाला interface।
  3. यह सोचना कि किसी lambda के अंदर this lambda को refer करता है, जबकि यह enclosing class instance को refer करता है।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.