Java के Lambda Expressions
In this page:
(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?
// 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();
}
}
Login to try C/C++/Java/PHP code in the editor
Lambda with Parameters
आप किसी lambda expression को parameters, arrow (->) operator के बाईं ओर parentheses के अंदर declare करके पास कर सकते हैं, बिल्कुल किसी normal method की parameter list की तरह, सिवाय इसके कि Java आमतौर पर उनके types को अपने आप infer कर सकता है।
उदाहरण: Lambda with Parameters
// 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));
}
}
Login to try C/C++/Java/PHP code in the editor
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
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));
}
}
Login to try C/C++/Java/PHP code in the editor
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
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();
}
}
Login to try C/C++/Java/PHP code in the editor
Common Use Cases
Lambda expressions सबसे आम तौर पर संक्षिप्त collection iteration, filtering predicates, या custom sorting comparators inline लिखने के लिए उपयोग होते हैं, ठीक वहां जहां उनकी ज़रूरत है, बजाय codebase में कहीं और बिखरी अलग named classes के।
उदाहरण: Common Use Cases
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);
}
}
Login to try C/C++/Java/PHP code in the editor
- किसी lambda के अंदर से एक local variable को modify करना, जैसे
count++, जो compile नहीं होता क्योंकि captured locals को final या effectively final होना चाहिए। - किसी lambda का उपयोग करना जहां target type एक functional interface नहीं है, जैसे
var f = () -> 5;या दो abstract methods वाला interface। - यह सोचना कि किसी lambda के अंदर
thislambda को refer करता है, जबकि यह enclosing class instance को refer करता है।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: