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 References as Lambdas

Java lambda implementation provides a shorter way to express the method call.  

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");

Above example passes println() method reference with the help of double colon (::). It tells the Java compile that method will be applied to the passed parameter of the 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

Popular posts from this blog

Java Streams API - Process set of elements

Java 8 Vs Java 9