Lambda Expression In Java

Hello Friends, Today we will discuss one more important concept of java which is “Lambda Expression” in Java.

Lambda expression was added in Java 8. We are already known Java is an object oriented programming language. There is a one more programming language called ‘Functional Programming Language’ but it is not supported by Java.

Now after implementation of Lambda expression, Java is supported Functional Programming Language. Main reason to implement lambda expression in java is to bring the functional programming features.

In Object oriented programming, all data stored in the form of objects and classes. But in functional programming language data is stored in the form of functions and variables. This is the basic difference.

Lambda expression is also used for code optimization.

 

What is Lambda expression in Java?

Lambda expression is an anonymous function in Java. A function doesn’t have name, return type and doesn’t return any value.

Example 1:

First I will write a function without using lambda expression.

Public void m(){
System.out.println(“welcome”);
}
  • Now using lambda expression:

() –> System.out.println(“welcome”);

 

Example 2:

Normal Function:

public void m(int a, int b){

System.out.println(a+b);
}

Using Lambda expression:

(a,b) –> System.out.println(a+b);

 

Example 3: If we are returning any value.

Public void m1(int a){
return(a*a);
}

Using Lambda expression:

  • (a) –> a*a;   OR      a –>a*a;

 

Now to invoke the lambda expression we need to know the functional interface.

What is Functional Interface?

An interface which contains only one abstract method is called functional interface. It is also known as single abstract method.

Examples: Runnable, Callable and Comparable. These are the functional interface because all have single abstract method.

Syntax of Functional Interface:

@functionalInterface
Interface I
{
Public void m1();
}

Example 1:

package Practice;

@FunctionalInterface
interface Cab{
    
    public void bookCab();
    
}

class Ola implements Cab{

    public void bookCab() {
        System.out.println("Ola cab is booked..");
        
    }
    
}

public class Test {
    
    public static void main(String[] args) {
        
        Cab cab = new Ola();
        cab.bookCab();
    }

}

 

Example 2:

package Encapsulation1;



@FunctionalInterface
interface Cab{
    
    public void bookCab(String Source, String Destination);
    

}


public class Test1 {
    
    public static void main(String[] args) {
        
        Cab cab = (String Source, String Destination)-> System.out.println("Ola cab is booked..");
        cab.bookCab("Pune" , "Mumbai");
        
    }

}


 

Lambda Expression Interview Question:

  1. (int a, int b) -> a+b; or (a, b) -> a + b; which one of these is a valid lambda expression?

Both of them are valid lambda expressions if used in a correct context.

With the first one (int a, int b) -> a+b; we know that the parameters must be of type int.

In case of (a, b) -> a + b; if used in a correct context type can be inferred from the context in which the lambda             expression is executed.

 

  1. (int a, b) -> a + b; is this a valid lambda expression?

you can’t have lambda expression where type for only one of the parameter is explicitly declared so this lambda expression is invalid.

3. Can a functional interface extend/inherit another interface?

A functional interface cannot extend another interface with abstract methods as it will void the rule of one      abstract method per functional interface. E.g:

interface Parent { 
public int parentMethod(); 
} 
@FunctionalInterface // This cannot be FunctionalInterface 
interface Child extends Parent { 
public int childMethod(); 
// It will also extend the abstract method of the Parent Interface 
// Hence it will have more than one abstract method 
// And will give a compiler error 
}

It can extend other interfaces which do not have any abstract method and only have the default, static, another class is overridden, and normal methods.

Example:

interface Parent { 
public void parentMethod(){ 
System.out.println("Hello"); 
} 
} 
@FunctionalInterface 
interface Child extends Parent { 
public int childMethod(); 
}

 

I hope you have enjoyed this tutorial. Happy Learning 🙂

 

Must Read:

What is Token in java?

Functional Interface in Java 8

Leave a Comment