JAVA 8


Java 8 lambdas

Why Lambdas?
  • Enables functional programming
  • Readable and concise code - eliminate the boilerplate code
  • Easier-to-use APIs and libraries.
  • Enables Support for parallel processing -
Functional Programming
  • read the more readable code or maintainable code
Problem in OOPS
  • Everything is an object
  • All code blocks are associated with classes and objects.

public class Greeter {
 public void greet(){
  System.out.println("Hello World");
 }
 public static void main(String[] args) {
  Greeter greeter = new Greeter();
  greeter.greet();
 }
}

Output:
Hello World

Passing Behavior in OOPS
The problem of the above program. greet() method always print the "Hello World" message.
If you want to print the different message on each time the greet method called. accept the argument. greet() method needs to know what to do. that is not an elegant design.

Have an interface as the argument itself and method needs to perform the behavior

Create an interface with the abstract operation.
Pass the interface as an argument in greet method
invoke the operation inside the greet method

public void greet(Greeting greeting){
 greeting.perform();
}
Greeting -> interface
perform -> method of the interface

public interface Greeting {
public void perform();
}

public class HelloGreeting implements Greeting {
public void perform() {
System.out.println("Hello World");

}
}
  

  

public class Greeter { 

 public void greet(Greeting greeting){

  greeting.perform(); 
 }
 public static void main(String[] args) {
  Greeter greeter = new Greeter();
  HelloGreeting greeting = new HelloGreeting();
  greeter.greet(greeting);
 }
 
 Lambda expression
inline values
ablockofcode = {
line1;
line2;
line3;
}
 
Assign a method to the variable
  
Function as value
ablockofcode = public void perform() {
System.out.println("Hello World");
}
A lot of the code does not need for lambda. You need to remove
  • remove the access modifier
  • remove the method name
  • remove the return type -> compiler identify the return type and return it.
Lambda expression with multiple line
ablockofcode = () -> {
System.out.println("Hello World");
}
Lambda expression with single line
ablockofcode = () -> System.out.println("Hello World");

Sample Lambda expression:
doubleNumberFunction = (int a) -> a * 2;
Get the int variable a and multiply by 2 and return the value;

addFunction = (int a, int b) -> a + b;
get a and b variable and add the values and return it.


safeDiv = (int a, int b) -> {
        if(b == 0) return 0;
             return a /b;
}


stringLengthCount = (String x) -> String.length();
Count the length of the string.
  • Create an interface
  • Have a method which matches the same signature of the lambdas expression.

public class Greeter {

public static void main(String[] args) {

MyLamdba myLamdaFunction = ()-> System.out.println("Hello world");

MyAdd addFunction = (int a, int b)-> a+b; 

}

}



interface MyLamdba{

void foo();

}


interface MyAdd{


int add (int x, int y);
}
Note: IDE should support the JDK 8 version.
Use different interface in Lambda. But Lamda expression should match with Signature of the method interface.


public interface Greeting {

public void perform();
}


Greeting myLambdaFuction = () -> System.out.println("HelloWorld");
Note: When you use Lambda expression. the interface should have a single method which matches the Lambda expression.



  

Comments

Popular posts from this blog

Robotic Process Automation

WorkFusion

7 Rules of Success A.P.J Abdul Kalam