Lambda expression in Java

Learn Java Lambda expression

 

 

coding-zon-java-lambda-expression

Lambda expression introduced in java8. It simplifies functional programming, and makes easier the development.

Lambda expression is an anonymous or unnamed function. It is used to implement a method defined by a function interface.

What is Functional Interface?

If a Java interface contains one and only one abstract method, then it is called as functional interface.

Difference between normal function and lambda.

It is simple and readable.
 

Normal function looks as shown below

void greet(){
    System.out.println("Hello");
}


Using lambda expression, it can be written as:

() → System.out.println("Hello");    // it is an anonymous function.

  

Defining the Lambda expression.


The arrow operator  (->) used is known as an arrow operator or a lambda operator. 

1. When there is a single parameter and single line, It can be written as shown

 

Syn:

 

 parameterlist ->  expression body 

 

Ex: 

 
() -> System.out.println("Hello");   
 
Ex2:  
 
String s = "Hello";
 
(n) -> System.out.println(n);  // with parameter.
 

TOP↑

 

 

2. when there is a multiple parameter and multiple lines, the body of the function enclosed in curly braces.

 
Syn: 
 
 (parameterlist) -> { expression body }
 
Ex:  

(a,b) -> {
         int c = a + b;
         System.out.println("result "+ c); 
         } 

 

So, you need to define a functional interface first with one abstract method. 

Then using lambda expression abstract method can be implemented 

 
 
 interface Person {
  String greet(String name);
}

public class Main {
  public static void main(String[] args) {
    Person  p1 = (name) -> "Hi " + name;

   f1("Ram", p1);
   
  }
  public static void f1(String name, Person pobj) {
    String result = pobj.greet(name);
    System.out.println(result);
  }
}
 
 
Ouput:
Hi Ram 
 
 

 

GO TOP↑

 

Q10 - Solution 

 
 
public class Demo {

   public static void main(String args[]) {
      Demo demo = new Demo();

     UserService userService1 = (fname,lname)  ->
      System.out.println("Fullname = " + (fname+" "+lname));
      userService1.fullName("Ram", "Kumar");
       
   }
   interface UserService {
      void fullName(String fname, String lname);
   }
 
}
 
 
 
Output:
Fullname = Ram Kumar
 
Ex2:
 

Q9 - Solution

 
  
public class Demo {

   public static void main(String args[]) {
      Demo demo = new Demo();

 
      Calculator cube = (a) -> a * a * a;
    System.out.println("result = " + demo.calculate(10, cube));

   }
	
   interface Calculator {
      int calculation(int n);
   }
	
    private int calculate(int n, Calculator  calobj) {
      return calobj.calculation(n);
   }
} 

 

Output:
result = 1000
 
  
 

MOVETOP↑

 

Following are the important points to be considered in the above example.

Lambda expressions defines inline implementation of a functional interface, i.e., an interface with a single method only.

In the above example, we've used a lambda expression to define the calculation method of the Calculator interface.

 
 

Q10 - Solution  - Collection ArrayList example

 
import java.util.ArrayList;
import java.util.Iterator;

class Demo {
  public static void main(String[] args){
    ArrayList<String> weekdays = new ArrayList<>();

    // Add elements in the array list
    weekdays.add("Sun");
    weekdays.add("Mon");
    weekdays.add("Tue");
    weekdays.add("Thu");
    weekdays.add("Fri");
    weekdays.add("Sat"); 

    // Create a variable of Iterator
    // store the iterator returned by iterator() method
    Iterator<String> iterate = weekdays.iterator();
    System.out.println("ArrayList: ");

    // loop through ArrayList until the last element
    // Use methods of Iterator to access elements
    while(iterate.hasNext()){
      System.out.println(iterate.next());
     // System.out.print(",");
    }
  }
  }
 

 

⬅Java QA 

 
 
 
 
 

Comments

Popular posts from this blog

Using javascript pass form variables to iframe src

Creating a new PDF by Merging PDF documents using TCPDF

Import excel file into mysql in PHP