Lambda Expressions

Lambda expressions provide the simplest and shortest way to define anonymous functions.  Java Swing frequently uses anonymous functions for event handling.

Lambda expressions define the body of Functional Interfaces.

An interface that has only one abstract method, is called Functional Interface. It can be annotated by @FunctionalInterface annotation.

Let's define HelloInt functional interface that has an abstract method say() :
@FunctionalInterface
interface HelloInt {
public void say();
}

Now define interface definition using old anonymous function approach: 

HelloInt aFun = new HelloInt() {
public void say() {
System.out.println("Hello Anonymous Function");
}
};

aFun.say();

Let's define the anonymous function using Lambda expression:  

HelloInt lambFun = () -> System.out.println("Hello Lambda");
lambFun.say();

The above example, clearly shown the shortest code of an anonymous function definition. The only condition is interface must have a single abstract method.  In other words, the interface must be a Functional Interface.

Few  most frequently used functional interfaces are  java.lang.Runnable and  java.lang.Comparable


Lambda Expression Syntax:

A lambda expression is composed of three parts.

 Argument List Arrow Token Body Remark
 () -> System.out.println("Hello Lambda") No parameters 
 x -> System.out.println(x) Single Parameters 
 (x, y) -> return x+y     Muti parameters 
 (x, y) ->{
     x +=2;
     return x*y;
}
 Body with multiple statements 


Lambda expressions may have zero or more arguments. The body can be either a single expression or a statement block. 

Let's define MathInt functional interface with multiple parameters.  It will have multiple lambda definitions to calculate add, subtract, and multiply of two numbers:

@FunctionalInterface
interface MathInt {
public int calculate(int a, int b);
}

MathInt add = (a, b) -> {
return a + b;
};
MathInt multiply = (a, b) -> {
return a * b;
};
MathInt subtract = (a, b) -> {
return a - b;
};

int a = 10;
int b = 5;
System.out.println("Add " + add.calculate(a, b));
System.out.println("Multiply " + multiply.calculate(a, b));
System.out.println("Suntract " + subtract.calculate(a, b));

Method Reference as Lambda

Java lambda implementation provides a shorter way to express a method call using the (::) operator that separates the method name from the name of an object or class in a method reference.

Here is an example of a method reference.
@FunctionalInterface
interface GreetingInt {
public void hello(String name);
}
GreetingInt g = System.out::println;
g.hello("Ram");
g.hello("Shyam");

The above example passes a reference of method println() with the help of a double colon (::). It tells the Java compiler that the method will be applied to the passed parameter of the lambda method.
GreetingInt g = System.out::println;
is the replacement of 
GreetingInt g = (n) -> System.out.println(n);

You can reference the following types of methods:
  1. Static method
  2. Instance method on parameter objects
  3. Instance method
  4. Constructor
Example Source Code



More ...

Also, Watch:

► Deploy Angular build on Tomcat or WAMP server - https://www.youtube.com/watch?v=0Gvx7...  
► SpringBoot Hibernate Integration - https://www.youtube.com/watch?v=IcSyY...  
► Spring Boot – Create RESTful Web Service - https://www.youtube.com/watch?v=QcoB0...  
► Angular app using REST API (Part 3) - https://www.youtube.com/watch?v=nthgO...  
► Test Public REST API - https://www.youtube.com/watch?v=SUE90...  
► Angular First Project (Part-2) - https://www.youtube.com/watch?v=ZySHh...  
► Angular First Project (Part-2) - https://www.youtube.com/watch?v=Y9aSJ...  
► First Python Program - https://www.youtube.com/watch?v=w_Z6l...  

FOLLOW US ON ALL OTHER SOCIAL PLATFORMS!

►YOUTUBE - https://www.youtube.com/sunilos  
►LINKEDIN - https://www.linkedin.com/company/rays-tec
►GITHUB - https://github.com/sunilos  
►FACEBOOK - https://www.facebook.com/sunilopensource/

►URL - www.RaysTec.com, www.SunilOS.com



Comments

  1. Are you looking for a reliable Hibernate Training in Delhi? Look no further than APTRON Delhi! With our comprehensive Hibernate training, you can master this popular Java framework and build enterprise-level applications with ease.

    ReplyDelete

Post a Comment

Popular posts from this blog

Java Streams API - Process set of elements

Java 8 Vs Java 9

Concurrent Collection in Java