10 August 2023

Lambda Expression In Java - learngreen.net

 

Lambda Expression in Java

Lambda Expression in Java

   The new and significant lambda expression feature of Java was added in Java SE 8. It offers a simple and direct approach to using an expression to represent one method interface. In a library's collection, it is quite helpful. It is advantageous to filter, iterate, and extract data from a collection.

The implementation of an interface with a functional interface is provided using the Lambda expression. Quite a bit of code is saved. In the case of a lambda expression, the method does not need to be defined again in order to provide the implementation. Just the implementation code is written here.

The compiler does not build a.class file because a Java lambda expression is viewed as a function.

A lambda expression is comparable to a brief piece of code that accepts input and output results. It somewhat resembles a method but doesn't require a name. Lambda expressions can be used directly within a method.

Lambda expressions act somewhat similarly to those functional interfaces you may have heard of in Java. These are unique interfaces that have just one primary function. We refer to them as lambda functions in Java. These lambda functions resemble small code bundles. They receive an input, process it, and then provide an output.

As evidenced by the capability for pipeline operations on data in the Stream API, lambda expressions make use of the parallel processing capabilities of multi-core systems.

They are methods without names used to implement methods specified by functional interfaces, or anonymous methods. Before getting your hands dirty with lambda expressions, it's critical to understand what a functional interface is

Functional interface implementation is provided by the lambda expression. A functional interface is one that only contains one abstract method. An interface can be designated as a functional interface using Java's annotation @FunctionalInterface.

To offer the functional interface's implementation.

less codes.

Lambda Expression Syntax

Parentheses are used to enclose a list of input parameters that are separated by commas. The inputs that will be provided to the lambda expression are represented by these parameters.

The parameter list and the body of the lambda expression are separated by the arrow character (->). It's comparable to being told, "With these inputs, do the following..."

Body:- A single expression or a group of statements encased in curly braces can serve as the lambda expression's body. The actual code that uses the input parameters to generate an output is defined here.

Lambda Expression Parameters



 //  1. Zero Parameter
 (  ) -> System.out.println("Hi");
 

 


 // 2. Single Parameter
 (parameter) -> System.out.println( parameter);
 

 


  // 3. Multiple Parameters
 (Multiple Parameters) -> System.out.println(Multiple Parameters);
 

 


 package Java;
 interface Exercisable {
 void exercise();
  }
 public class ExerciseLambdaExample {
 public static void main(String[] args) {
 int minutesToExercise = 30;
 // Lambda expression for exercising
 Exercisable exercising = () -> {
 System.out.println
 ("Exercising for "+minutesToExercise + " minutes.");
     };
 exercising.exercise();
	}
 }
 //Output-> Exercising for 30 minutes.

 


 package Java;
 //Define a functional interface named "fruit"
 interface fruit {
 // Abstract method declaration	 
 public String method(); 
 }
 public class SentenceLambdaExample {
 public static void main(String[] args) {
 /* Create an instance of the "fruit"
  * interface using a lambda expression 
  */
 fruit obj = ()-> {
 return "Eating fruits is good for health.";
 };
 /* Call the "method" abstract method
  * of the interface using the instance
  */
 System.out.println(obj.method());
 /* Output->
  * Eating fruits is good for health
  */
  }
 }

 


/*    Lambda expression having
  *    single parameter
  */
package Java;
 /* Define a functional
  * interface named "Eatfruit"
  */
 interface Eatfruit {
 /* Abstract method declaration
  * with a parameter "fruit"	 
  */
 public String method(String fruit); 
 /* Create an instance of the
  * "Eatfruit" interface using
  * a lambda expression
  */
  }
 public class LambdaExample {
 public static void main(String[] args) {
 Eatfruit obj = (Fruitname)-> {
 return "Consume an "+Fruitname+" daily.";
   };
 /* Call the "method" abstract method
  * of the interface using the instance
  */
 System.out.println(obj.method("Apple"));
 /* Output->
  * Consume an Apple daily.
  */
	}
 }

 


/*    Lambda expression having
  *    Multiple parameter
  */

 package Java;
 /* Define a functional
  * interface named "Numbers"
  */
 interface Numbers {
 /* Abstract method 
  * declaration with two parameters */	 
 int multiply(int x,int y); 
 }
 public class LambdaExample1 {
 public static void main(String[] args) {
 /* Create an instance of the "Numbers"
  * interface using a lambda expression */	 
 Numbers obj =(x,y)-> (x*y);
 /* Call the "multiply" method
  * of the interface using the
  * instance and print the result */
 System.out.println(obj.multiply(5,4));
 
 //Output-> 20
	}
  }

 

No comments:

Post a Comment