Lambdas (Java 8+) संक्षिप्त anonymous functions हुन्, र functional interface एक interface हो जसमा ठीक एक abstract method हुन्छ — lambda ले implement गर्ने target type। यिनले Java लाई functional programming ल्याए र Stream API लाई शक्तिशाली बनाएका छन्।
एक functional interface
@FunctionalInterface // optional annotation — enforces ONE abstract method
interface Calculator {
int calculate(int a, int b); // the single abstract method
}
एक abstract method भएको interface "functional" हुन्छ — एक lambda यो implement गर्न सक्छ किनकि कुन method lambda प्रतिनिधित्व गर्छ यसमा कोनो संदेह छैन।
Lambda syntax — यसलाई संक्षिप्तमा implement गर्नुहोस्
// ❌ verbose anonymous class (pre-Java 8)
Calculator add = new Calculator() {
public int calculate(int a, int b) { return a + b; }
};
// ✅ lambda — same thing, far less boilerplate
Calculator add = (a, b) -> a + b;
Calculator multiply = (a, b) -> a * b;
add.calculate(2, 3); // 5
Lambda (a, b) -> a + b ले एकल method को implementation प्रदान गर्छ — कोनो class छैन, कोनो method signature boilerplate छैन।
Built-in functional interfaces (java.util.function)
Function<String, Integer> length = s -> s.length(); // takes T, returns R
Predicate<Integer> isPositive = n -> n > 0; // takes T, returns boolean
Consumer<String> printer = s -> System.out.println(s); // takes T, returns void
Supplier<String> greeting = () -> "Hello"; // takes nothing, returns T
BiFunction<Integer, Integer, Integer> sum = (a, b) -> a + b; // two args
Java ले सामान्य आकारहरूको लागि तयार-बनेका functional interfaces प्रदान गर्छ, जुन मानक library भरि (विशेषगरी streams) प्रयोग हुन्छन्।
Method references — अझ पनि छोटो
// when a lambda just calls an existing method, use a method reference
list.forEach(System.out::println); // instead of x -> System.out.println(x)
list.stream().map(String::toUpperCase); // instead of s -> s.toUpperCase()
list.stream().map(Integer::parseInt); // static method ref
जहाँ lambdas चमकता है
// passing behavior as arguments — the core enabler of the Stream API
people.stream()
.filter(p -> p.getAge() > 18) // Predicate lambda
.map(Person::getName) // method reference
.forEach(System.out::println); // Consumer
list.sort((a, b) -> a.compareTo(b)); // Comparator lambda
button.addActionListener(e -> handleClick()); // event handler
यो महत्वपूर्ण किन छ
Lambdas र functional interfaces ले Java लाई functional programming ल्याए, आधुनिक Java कसरी लेखिन्छ यो मौलिक रूपमा परिवर्तन गरे।
यिनले verbose anonymous-class boilerplate लाई हटाएको छ behavior को डेटा हिसाबले पास गर्ने को लागि, event handlers, comparators, callbacks लाई संक्षिप्त बनाएको छ, र — सबभन्दा महत्वपूर्ण — सम्पूरा Stream API (जो lambdas मा निर्भर हुन्छ यसको filter/map/reduce operations को लागि)। /यो बुझ्नु कि एक lambda एक functional interface (एक abstract method) लाई target गर्छ, built-in interfaces (Function, Predicate, Consumer, Supplier), र method references आधुनिक Java लाई idiomatic तरिकामा लेख्न र streams, collections, र concurrent APIs प्रभावकारी रूपमा प्रयोग गर्नको लागि आवश्यक हुन्छ।
यिनले भाषाको एक प्रमुख विकास प्रतिनिधित्व गर्छन् र हालको Java fluency को कोर हुन्छन्।
