जावा लेम्बडा एक्सप्रेशन (उदाहरण के साथ)

इस लेख में, हम जावा लैम्ब्डा अभिव्यक्ति और कार्यात्मक इंटरफेस के साथ लैम्ब्डा एक्सप्रेशन के उपयोग, जेनेरिक फंक्शनल इंटरफेस और उदाहरणों की मदद से एपीआई स्ट्रीम के बारे में जानेंगे।

लैम्ब्डा अभिव्यक्ति को पहली बार जावा 8 में पेश किया गया था। इसका मुख्य उद्देश्य भाषा की अभिव्यंजक शक्ति को बढ़ाना है।

लेकिन, लंबोदर में जाने से पहले, हमें सबसे पहले कार्यात्मक इंटरफेस को समझने की आवश्यकता है।

कार्यात्मक इंटरफ़ेस क्या है?

यदि जावा इंटरफ़ेस में एक और केवल एक सार पद्धति है तो इसे कार्यात्मक इंटरफ़ेस कहा जाता है। यह केवल एक विधि इंटरफ़ेस के इच्छित उद्देश्य को निर्दिष्ट करता है।

उदाहरण के लिए, Runnableपैकेज से इंटरफ़ेस java.lang; एक कार्यात्मक इंटरफ़ेस है क्योंकि यह केवल एक विधि का गठन करता है run()

उदाहरण 1: जावा में एक कार्यात्मक इंटरफ़ेस परिभाषित करें

 import java.lang.FunctionalInterface; @FunctionalInterface public interface MyInterface( // the single abstract method double getValue(); )

उपरोक्त उदाहरण में, इंटरफ़ेस MyInterface में केवल एक सार विधि getValue () है। इसलिए, यह एक कार्यात्मक इंटरफ़ेस है।

यहाँ, हमने एनोटेशन का उपयोग किया है @FunctionalInterface। एनोटेशन जावा कंपाइलर को इंगित करने के लिए मजबूर करता है कि इंटरफ़ेस एक कार्यात्मक इंटरफ़ेस है। इसलिए, एक से अधिक अमूर्त पद्धति की अनुमति नहीं देता है। हालाँकि, यह अनिवार्य नहीं है।

जावा 7 में, कार्यात्मक इंटरफेस को एकल सार तरीके या एसएएम प्रकार माना जाता था । आमतौर पर जावा 7 में बेनामी कक्षाओं के साथ एसएएम को लागू किया गया था।

उदाहरण 2: एसएएम को जावा में अनाम कक्षाओं के साथ लागू करें

 public class FunctionInterfaceTest ( public static void main(String() args) ( // anonymous class new Thread(new Runnable() ( @Override public void run() ( System.out.println("I just implemented the Runnable Functional Interface."); ) )).start(); ) )

आउटपुट :

 मैंने सिर्फ रननेबल फंक्शनल इंटरफेस लागू किया है।

यहां, हम एक अनाम वर्ग को एक विधि में पास कर सकते हैं। यह जावा 7 में कम कोड वाले प्रोग्राम लिखने में मदद करता है। हालांकि, सिंटैक्स अभी भी मुश्किल था और कोड की बहुत सारी अतिरिक्त लाइनों की आवश्यकता थी।

जावा 8 ने एक कदम आगे जाकर एसएएम की शक्ति को बढ़ाया। चूंकि हम जानते हैं कि एक कार्यात्मक इंटरफ़ेस में सिर्फ एक विधि होती है, इसे तर्क के रूप में पारित करते समय उस पद्धति के नाम को परिभाषित करने की आवश्यकता नहीं होनी चाहिए। लैम्ब्डा अभिव्यक्ति हमें वास्तव में ऐसा करने की अनुमति देती है।

लंबोदर भावों का परिचय

लैम्ब्डा अभिव्यक्ति, अनिवार्य रूप से, एक अनाम या अनाम विधि है। लैम्ब्डा अभिव्यक्ति अपने दम पर निष्पादित नहीं करता है। इसके बजाय, यह एक कार्यात्मक इंटरफ़ेस द्वारा परिभाषित विधि को लागू करने के लिए उपयोग किया जाता है।

जावा में लैम्ब्डा अभिव्यक्ति कैसे परिभाषित करें?

यहाँ हम जावा में लैम्ब्डा एक्सप्रेशन को कैसे परिभाषित कर सकते हैं।

 (parameter list) -> lambda body

->उपयोग किए गए नए ऑपरेटर ( ) को एक तीर ऑपरेटर या एक लंबो ऑपरेटर के रूप में जाना जाता है। वाक्य-विन्यास इस समय स्पष्ट नहीं हो सकता है। आइए कुछ उदाहरणों का अन्वेषण करें,

मान लीजिए, हमारे पास एक तरीका है:

 double getPiValue() ( return 3.1415; )

हम इस विधि को लंबोदर अभिव्यक्ति के रूप में लिख सकते हैं:

 () -> 3.1415

यहां, विधि में कोई पैरामीटर नहीं है। इसलिए, ऑपरेटर के बाईं ओर एक खाली पैरामीटर शामिल है। दाईं ओर लैम्ब्डा बॉडी है जो लैम्बडा एक्सप्रेशन की क्रिया को निर्दिष्ट करती है। इस स्थिति में, यह 3.1415 मान लौटाता है।

लैम्ब्डा बॉडी के प्रकार

जावा में, लैम्ब्डा बॉडी दो प्रकार की होती है।

1. एकल अभिव्यक्ति वाला एक शरीर

 () -> System.out.println("Lambdas are great");

इस प्रकार के लंबोदर शरीर को अभिव्यक्ति शरीर के रूप में जाना जाता है।

2. एक निकाय जिसमें कोड का एक ब्लॉक होता है।

 () -> ( double pi = 3.1415; return pi; );

इस प्रकार के लंबोदर शरीर को ब्लॉक बॉडी के रूप में जाना जाता है। ब्लॉक बॉडी लैम्ब्डा बॉडी को कई स्टेटमेंट को शामिल करने की अनुमति देती है। ये कथन ब्रेसिज़ के अंदर संलग्न हैं और आपको ब्रेसिज़ के बाद एक अर्ध-उपनिवेश जोड़ना होगा।

नोट : ब्लॉक बॉडी के लिए, आपके पास रिटर्न स्टेटमेंट हो सकता है यदि बॉडी एक वैल्यू लौटाती है। हालांकि, अभिव्यक्ति निकाय को रिटर्न स्टेटमेंट की आवश्यकता नहीं होती है।

उदाहरण 3: लैम्ब्डा एक्सप्रेशन

चलो एक जावा प्रोग्राम लिखते हैं जो लंबोदर अभिव्यक्ति का उपयोग करके पाई का मान लौटाता है।

जैसा कि पहले उल्लेख किया गया है, एक मेमने की अभिव्यक्ति को अपने दम पर निष्पादित नहीं किया जाता है। बल्कि, यह कार्यात्मक इंटरफ़ेस द्वारा परिभाषित सार पद्धति के कार्यान्वयन को बनाता है।

So, we need to define a functional interface first.

 import java.lang.FunctionalInterface; // this is functional interface @FunctionalInterface interface MyInterface( // abstract method double getPiValue(); ) public class Main ( public static void main( String() args ) ( // declare a reference to MyInterface MyInterface ref; // lambda expression ref = () -> 3.1415; System.out.println("Value of Pi = " + ref.getPiValue()); ) )

Output:

 Value of Pi = 3.1415

In the above example,

  • We have created a functional interface named MyInterface. It contains a single abstract method named getPiValue()
  • Inside the Main class, we have declared a reference to MyInterface. Note that we can declare a reference of an interface but we cannot instantiate an interface. That is,
     // it will throw an error MyInterface ref = new myInterface(); // it is valid MyInterface ref;
  • We then assigned a lambda expression to the reference.
     ref = () -> 3.1415;
  • Finally, we call the method getPiValue() using the reference interface. When
     System.out.println("Value of Pi = " + ref.getPiValue());

Lambda Expressions with parameters

Till now we have created lambda expressions without any parameters. However, similar to methods, lambda expressions can also have parameters. For example,

 (n) -> (n%2)==0

Here, the variable n inside the parenthesis is a parameter passed to the lambda expression. The lambda body takes the parameter and checks if it is even or odd.

Example 4: Using lambda expression with parameters

 @FunctionalInterface interface MyInterface ( // abstract method String reverse(String n); ) public class Main ( public static void main( String() args ) ( // declare a reference to MyInterface // assign a lambda expression to the reference MyInterface ref = (str) -> ( String result = ""; for (int i = str.length()-1; i>= 0 ; i--) result += str.charAt(i); return result; ); // call the method of the interface System.out.println("Lambda reversed = " + ref.reverse("Lambda")); ) )

Output:

 Lambda reversed = adbmaL

Generic Functional Interface

Till now we have used the functional interface that accepts only one type of value. For example,

 @FunctionalInterface interface MyInterface ( String reverseString(String n); )

The above functional interface only accepts String and returns String. However, we can make the functional interface generic, so that any data type is accepted. If you are not sure about generics, visit Java Generics.

Example 5: Generic Functional Interface and Lambda Expressions

 // GenericInterface.java @FunctionalInterface interface GenericInterface ( // generic method T func(T t); ) // GenericLambda.java public class Main ( public static void main( String() args ) ( // declare a reference to GenericInterface // the GenericInterface operates on String data // assign a lambda expression to it GenericInterface reverse = (str) -> ( String result = ""; for (int i = str.length()-1; i>= 0 ; i--) result += str.charAt(i); return result; ); System.out.println("Lambda reversed = " + reverse.func("Lambda")); // declare another reference to GenericInterface // the GenericInterface operates on Integer data // assign a lambda expression to it GenericInterface factorial = (n) -> ( int result = 1; for (int i = 1; i <= n; i++) result = i * result; return result; ); System.out.println("factorial of 5 = " + factorial.func(5)); ) )

Output:

 Lambda reversed = adbmaL factorial of 5 = 120

In the above example, we have created a generic functional interface named GenericInterface. It contains a generic method named func().

Here, inside the Main class,

  • GenericInterface reverse - creates a reference to the interface. The interface now operates on String type of data.
  • GenericInterface factorial - creates a reference to the interface. The interface, in this case, operates on the Integer type of data.

Lambda Expression and Stream API

The new java.util.stream package has been added to JDK8 which allows java developers to perform operations like search, filter, map, reduce, or manipulate collections like Lists.

For example, we have a stream of data (in our case a List of String) where each string is a combination of country name and place of the country. Now, we can process this stream of data and retrieve only the places from Nepal.

For this, we can perform bulk operations in the stream by the combination of Stream API and Lambda expression.

Example 6: Demonstration of using lambdas with the Stream API

 import java.util.ArrayList; import java.util.List; public class StreamMain ( // create an object of list using ArrayList static List places = new ArrayList(); // preparing our data public static List getPlaces()( // add places and country to the list places.add("Nepal, Kathmandu"); places.add("Nepal, Pokhara"); places.add("India, Delhi"); places.add("USA, New York"); places.add("Africa, Nigeria"); return places; ) public static void main( String() args ) ( List myPlaces = getPlaces(); System.out.println("Places from Nepal:"); // Filter places from Nepal myPlaces.stream() .filter((p) -> p.startsWith("Nepal")) .map((p) -> p.toUpperCase()) .sorted() .forEach((p) -> System.out.println(p)); ) )

Output:

 Places from Nepal: NEPAL, KATHMANDU NEPAL, POKHARA

In the above example, notice the statement,

 myPlaces.stream() .filter((p) -> p.startsWith("Nepal")) .map((p) -> p.toUpperCase()) .sorted() .forEach((p) -> System.out.println(p));

Here, we are using the methods like filter(), map() and forEach() of the Stream API. These methods can take a lambda expression as input.

हम उपर्युक्त सीखे गए सिंटैक्स के आधार पर अपने स्वयं के भावों को भी परिभाषित कर सकते हैं। यह हमें कोड की लाइनों को काफी कम करने की अनुमति देता है जैसा कि हमने उपरोक्त उदाहरण में देखा था।

दिलचस्प लेख...